上下文处理器与模板标签的最佳实践 [英] Best Practice for Context Processors vs. Template Tags?

查看:89
本文介绍了上下文处理器与模板标签的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建模板标签(并将其加载到模板中)的情况下,比创建上下文处理器(自动填充请求)更好吗?

In which cases is it better to create template tags (and load them into the template), than creating a context processor (which fills the request automatically)?

例如我有一个动态菜单,必须包含在所有的模板,所以我把它放入我的base.html。什么是首选用法:上下文处理器或自定义模板标签? 为什么

e.g. I have a dynamic menu that has to be included into all templates, so I'm putting it into my base.html. What is the preferred usage: context processor or custom template tag? And why?

推荐答案

上下文处理器用于将数据(信息,内容,对象)用于呈现页面的上下文。

Context processors is for putting data (information, content, objects) into the context used to render page.

模板标签用于格式化或处理该内容。

Template tags are for formatting or processing that content.

组成新数据令人困惑。不是不可能或错误,但非常混乱。

A template tag that makes up new data is confusing. Not impossible or wrong, but very confusing.

这篇关于上下文处理器与模板标签的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆