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

问题描述

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

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.

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

07-05 00:19