可重复使用的 HTML 代码/小部件

发布于 2024-09-13 19:56:54 字数 232 浏览 2 评论 0原文

有时我需要在不同的模板中使用相同的 html 代码,例如:

<div class="mylist"><span>item-1</span><span>item-2</span>...</div>

或更复杂的小部件。我是 Django 新手,所以我想了解在这种情况下你通常会做什么?您创建自己的模板标签还是什么?

Sometimes I need to use the same html code in different templates, like:

<div class="mylist"><span>item-1</span><span>item-2</span>...</div>

or more complicated widgets. I'm new to Django so I want to learn what do you usually do in these kinds of situations? Do you create your own template tags or what?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉风有信 2024-09-20 19:56:54

听起来您正在寻找包含标签, http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#inclusion-tags

包含标签是一种非常简单的模板标签类型,用于将标记和代码分解到其他模板中。这应该符合您的示例。

如果最终需要的话,您可以编写更复杂的模板标签来完成几乎所有您需要的其他操作,但包含标签是一个很好的起点。

It sounds like you're after an inclusion tag, http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#inclusion-tags .

An inclusion tag is a very simple type of template tag for breaking out markup and code into other templates. This should fit the bill for your example.

You can write more complicated template tags to do pretty much anything else you need if you need to eventually, but inclusion tags are a good place to start.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文