当我将模板函数作为基类的模板参数传递时,链接器会抱怨它无法链接该函数:
#include <stdio.h>
template<int I> inline int identity() {return I;}
//template<> inline int identity<10>() {return 20;}
template<int (*fn)()>
class Base {
public:
int f() {
return fn();
}
};
template<int Val>
class Derived : public Base<identity<10> > {
public:
int f2() {
return f();
}
};
int main(int argc, char **argv) {
Derived<10> o;
printf("result: %d\n", o.f2());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
结果是:
$ g++ -o test2 test2.cpp && ./test2
/tmp/ccahIuzY.o: In function `Base<&(int identity<10>())>::f()':
test2.cpp:(.text._ZN4BaseIXadL_Z8identityILi10EEivEEE1fEv[_ZN4BaseIXadL_Z8identityILi10EEivEEE1fEv]+0xd): undefined reference to `int identity<10>()'
collect2: …Run Code Online (Sandbox Code Playgroud) 我的一个jinja模板中的代码与以下内容类似
{% for post in posts %}
{% include ["posts/" + post.type + ".html", "posts/default.html"] %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
根据帖子的不同,它应该post在posts集合中呈现每个集合.type.我为每个人设置了不同的模板post.type.对于那些我没有模板的人,它会恢复到default帖子模板.
现在,我希望帖子的索引从帖子模板的底部显示,由提供loop.revindex.但出于某种原因,如果我loop.revindex在帖子模板中使用,我会收到错误说法UndefinedError: 'loop' is undefined.
那么,是loop不是在使用included模板?这是设计的吗?我是否在组织模板以使其无法使用时出错?
编辑好的,我在for循环中提出了一个解决方法,在我包含模板之前,我做了
{% set post_index = loop.revindex %}
Run Code Online (Sandbox Code Playgroud)
并post_index在帖子模板中使用.不理想,但似乎是唯一的方式.我仍然想知道你的解决方案.
编辑2的另一件事,我能够访问post内部变量included模板,而不是loop变量.
是否有一个现有的好例子,或者如何创建一个支持PHP5中的"模板继承"的基本模板系统(思考MVC)?
有关我定义为模板继承的示例,请参阅Django(用于Web开发的Python框架)模板文档:http://docs.djangoproject.com/en/dev/topics/templates/#id1
我特别喜欢PHP本身是"模板语言"的想法,尽管它不一定是必需的.
如果列出实施"模板继承"的现有解决方案,请尝试将答案形成为单独的系统,以利于"普遍投票".
我有一个场景,我有一个定义标题部分的主模板.看起来像这样......
<!DOCTYPE html>
<html>
<head>
@section('header')
{{ HTML::style('css/planesaleing.css') }}
{{ HTML::script('js/jquery-1.10.1.js') }}
{{ HTML::script('js/search_Bar.js') }}
@show
</head>
<body>
<div class="planesaleing_page">
<header>
@yield('header_bar')
@yield('nav_bar')
@yield('search_bar')
</header>
<div class="main_page">
// Some more code
</div>
@yield('footer')
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
如您所见,我有几个子视图(例如nav_bar和search_bar).每个子视图都有一个附带的.js文件.所以我想扩展nav_bar中的'header'部分,就像这样......
@section('header')
@parent
{{ HTML::script('js/anotherjs.js') }}
@stop
Run Code Online (Sandbox Code Playgroud)
然后再次在search_bar中这样:
@section('header')
@parent
{{ HTML::script('js/yetanotherjs.js') }}
@stop
Run Code Online (Sandbox Code Playgroud)
目的是最终输出的html文件如下所示:
@section('header'){{HTML :: style('css/planesaleing.css')}} {{HTML :: script('js/jquery-1.10.1.js')}} {{HTML :: script('js/search_Bar.js')}} {{HTML :: script('js/anotherjs.js')}} {{HTML :: script('js/yetanotherjs.js')}} @show
但是,只有第一个实际扩展了标题,之后的所有其他标题似乎都被忽略了.无论如何使用多个扩展?
任何建议 - 非常感谢.
我如何使用布局、partials 和如下所示的把手模板?
我查看了部分文档,但仍然无法弄清楚我想要实现的目标。
默认.html
默认布局被重复用于站点的不同视图。{{{content}}}用作主要内容将被呈现的位置的占位符。
<!DOCTYPE html>
<html>
<head>
<title>{{title}}</title>
</head>
<body>
<p>This is the top of the body content in the default.html file</p>
{{{content}}}
<p>This is the bottom of the body content in the default.html file</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
索引.html
{{#> default}}
{{ include header }}
<p>This is some other content</p>
{{ include footer }}
Run Code Online (Sandbox Code Playgroud)
标题.html
<h1>This is a header</h1>
Run Code Online (Sandbox Code Playgroud)
页脚.html
<p>This is a footer</p>
Run Code Online (Sandbox Code Playgroud)
输出
<!DOCTYPE html>
<html>
<head>
<title>Using Layout, Partials with Handlebars Template</title> …Run Code Online (Sandbox Code Playgroud) html javascript template-engine template-inheritance handlebars.js
所以这个例子来自:http://en.cppreference.com/w/cpp/utility/variant/visit声明了专门的类型:
template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
Run Code Online (Sandbox Code Playgroud)
这里构造为r值:
std::visit(overloaded {
[](auto arg) { std::cout << arg << ' '; },
[](double arg) { std::cout << std::fixed << arg << ' '; },
[](const std::string& arg) { std::cout << std::quoted(arg) << ' '; },
}, v);
Run Code Online (Sandbox Code Playgroud)
我试图弄清楚它是如何工作的.overloaded从这里继承的类型是什么?它看起来像是一群lambdas,但我不知道它会如何operator().有人可以解释继承在这里是如何工作的吗?
c++ operators specialization template-inheritance variadic-templates
我有一个base.html模板,其中包含用于charset,google-site-verification,stylesheets,js的全站点标签....我还需要为页面特定的标题标签和元描述设置块.
我想知道,我应该在我的base.html和我的继承模板中设置{%block head%},或者我应该设置特定的块,例如{%block meta%}和{%block标题%}以便当Django呈现为html时,标记出现在适当的位置.
这有意义吗?如果我查看混合在一个{%block head%}中的所有标签的来源,事情有点乱,但如果我为每个标签添加特定的块,它们按顺序但是使用了更多的代码......?
我想拆分模板参数包。像这样的东西。我怎么能去做这件事?
template< typename... Pack >
struct TypeB : public TypeA< get<0, sizeof...(Pack)/2>(Pack...) >
, public TypeA< get<sizeof...(Pack)/2, sizeof...(Pack)>(Pack...) >
{
};
Run Code Online (Sandbox Code Playgroud)
这是我对为什么这个问题不重复的看法:我正在寻找一种通用的方法来做到这一点,当将拆分包传递给其他模板类时,该方法将起作用 - 如上所示。以及将其传递给函数。
c++ templates template-inheritance template-meta-programming variadic-templates
我在我的工作中开发了一个库,我设计了一个复杂的继承,包括模板类并从中派生出来.我的问题是基本模板类有虚拟重载运算符,它接受2个参数并返回一些值.在基类中,实现了此运算符,并且大多数派生类不会重新实现此运算符.
其他一些类使用派生类进行某些工作并使用其运算符成员函数.只要派生类没有其他重载运算符,即使使用不同数量的参数,一切都可以正常工作.如果有,那么基类操作符不能使用它,object()因为编译器找不到合适的成员函数(抱怨参数计数不匹配).
无论是否指定基类的默认模板参数都无关紧要.派生类的定义顺序也不会改变哪个运算符导致问题(它总是SpecificDerived类).
下面我提出简化问题.
[编辑]简化了示例
基类定义:
template<class ret_t>
class TemplateBase2
{
public:
virtual ~TemplateBase2()
{
}
virtual ret_t memberFunc(ret_t x)
{
return x * 2;
}
};
Run Code Online (Sandbox Code Playgroud)
派生类定义的用户:
template <class worker, class ret_t>
ret_t gobble(worker w, float f)
{
return w.memberFunc((ret_t)f);
}
Run Code Online (Sandbox Code Playgroud)
派生类:
class SpecificDerived2: public TemplateBase2<float>
{
public:
float memberFunc()
{
return 3.14;
}
};
Run Code Online (Sandbox Code Playgroud)
主功能:
#include <iostream>
#include "TemplateBase2.h"
using namespace std;
int main()
{
SpecificDerived2 sd2;
cout << "sd2: " << gobble<SpecificDerived2, float>(sd2, …Run Code Online (Sandbox Code Playgroud) 我有三个html文件:
在page.html中我扩展了base.html.在comment.html中,我扩展了page.html.将comment.html扩展base.html文件的块?
django inheritance django-templates template-inheritance django-inheritance
我正在尝试创建 base.html 并在基础上加载几个名为“nav.html”、“contents.html”和“footer.html”的子模板。每当我访问 /base.html 时,我想让所有三个子模板都加载到 base.html 页面上。
基本.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>{% block title %}{% endblock %}</title>
</head>
<body>
<nav class="navbar">
<div class="nav">
{% block nav %}{% endblock %}
</div>
</nav>
<div class="content">
{% block contents %}{% endblock %}
</div>
<div class="footer">
{% block footer %}{% endblock %}
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
导航.html:
{% extends "base.html" %}
{% block title %}Page Title{% endblock %}
{% block nav %}
<p>Here goes the nav</p>
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
内容.html:
{% …Run Code Online (Sandbox Code Playgroud) django extends multiple-inheritance django-templates template-inheritance
我正在使用Django 1.1,我有这个模板,一个基本模板,所有其他页面都继承自.它定义了一堆在整个网站中都是不变的东西,比如这个导航栏:
<div id="navbar">
{% block navbar %}
<a href="">Link 1</a>
<a href="">Link 2</a>
<a href="">Link 3</a>
<a href="">Link 4</a>
<a href="/admin/">Admin</a>
{% endblock %}
</div>
Run Code Online (Sandbox Code Playgroud)
但Django在子模板中的默认行为是让子项完全覆盖父模板中的块.我这里有这个页面,不一定要覆盖导航栏块,只需添加一些特定于该页面的条目,但是现在我能看到发生的唯一方法就是我是从父母复制导航栏块,然后将其包含在模板+我的添加中.还有其他方法可以做吗?
我了解了Django模板继承并正在研究它。
我base_post_login.html在与其他模板相同的目录中制作了一个模板。
并{% extends "base_post_login.html" %}在子模板中输入第一行。
但是当通过后端呈现子模板时,TemplateDoesNotExist会引发错误。
这是settings.py(相关部分):
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Run Code Online (Sandbox Code Playgroud)
如果未扩展,则所有模板均正确渲染;如果是父模板,则所有模板均正确呈现。
我该怎么办?