Questions tagged [templates]

The templates tag is used in multiple contexts: generic programming (especially C++), and data/document generation using template engines, web template for a pre-designed webpage, or set of HTML webpages. When using this tag on implementation heavy questions - tag the code language the implementation is written in.

The templates tag is used in multiple contexts:

###C++ templates

Templates in allow for generic programming and . The C++ Book Guide contains books treating templates as well, especially:

Before asking a question, consider looking into these FAQs first:

There are also useful questions on StackOverflow:

Books

###Other templates (PHP, django, drupal, mediawiki, etc.)

There are several varieties of template engines used with web servers, web applications, and web scripting languages. Questions for these types of templates should use the language specific tag.

The web server or scripting language templates are different from templates as used in or generics as used in . These templates are used to help with separating view or presentation of data with the business logic generating or transforming the data.

PHP template questions should use the specific template product tag such as , , etc.

django template questions should use .

drupal template questions should use .

mediawiki template questions should use .

54454 questions
149
votes
6 answers

How to use comments in Handlebar templates?

I am using Handlebar.js as my templating engine. Now I want to comment out some of the blocks in my handlebar templates. But then I realized that Handlebar doesn't ignore the expressions inside the Handlebar comment block. Any workaround for this?
Abhidev
  • 7,063
  • 6
  • 21
  • 26
149
votes
4 answers

How to forward declare a C++ template class?

Given a template class like the following: template class Mappings { public: ... Type valueFor(const IDType& id) { // return value } ... }; How can someone forward declare this class…
Tron Thomas
  • 1,501
  • 2
  • 9
  • 3
149
votes
4 answers

Does it make any sense to use inline keyword with templates?

Since templates are defined within headers and compiler is able to determine if inlining a function is advantageous, does it make any sense? I've heard that modern compilers know better when to inline a function and are ignoring inline hint. edit:…
mip
  • 8,355
  • 6
  • 53
  • 72
147
votes
10 answers

C++ template constructor

I wish to have a non-template class with a template constructor with no arguments. As far as I understand, it's impossible to have it (because it would conflict with the default constructor - am I right?), and the workaround is the following: class…
Yippie-Ki-Yay
  • 22,026
  • 26
  • 90
  • 148
145
votes
13 answers

How to emulate C array initialization "int arr[] = { e1, e2, e3, ... }" behaviour with std::array?

(Note: This question is about not having to specify the number of elements and still allow nested types to be directly initialized.) This question discusses the uses left for a C array like int arr[20];. On his answer, @James Kanze shows one of the…
Xeo
  • 129,499
  • 52
  • 291
  • 397
145
votes
3 answers

How do I explicitly instantiate a template function?

I have a template function with one argument. I have to instantiate that function without calling that function means explicitly I have to instantiate. I have this function: template int function_name(T a) {} I instantiated that function…
Balaji
  • 1,459
  • 2
  • 11
  • 4
145
votes
5 answers

Using ngIf without an extra element in Angular 2

Can I use ngIf without an extra container element?
...
...
It doesn't work in a table because that would make invalid HTML.
janispritzkau
  • 1,877
  • 2
  • 12
  • 15
144
votes
4 answers

What does template mean?

When declaring a template, I am used to having this kind of code: template But in this question, they used: template I checked that it compiles. But what does it mean? Is it a non-type parameter? And if so, how can we…
Igor
  • 26,650
  • 27
  • 89
  • 114
144
votes
11 answers

Why can't I use float value as a template parameter?

When I try to use float as a template parameter, the compiler cries for this code, while int works fine. Is it because I cannot use float as a template parameter? #include using namespace std; template class…
yokks
  • 5,683
  • 9
  • 41
  • 48
142
votes
4 answers

Modulus % in Django template

I'm looking for a way to use something like the modulus operator in django. What I am trying to do is to add a classname to every fourth element in a loop. With modulus it would look like this: {% for p in posts %}
underdoeg
  • 1,862
  • 4
  • 14
  • 26
140
votes
6 answers

Inheriting from a template class in c++

Let's say we have a template class Area, which has a member variable T area, a T getArea() and a void setArea(T) member functions. I can create an Area object of a specific type by typing Area. Now I have a class Rectangle that inherits the…
dtech
  • 47,916
  • 17
  • 112
  • 190
140
votes
9 answers

How can I output the value of an enum class in C++11

How can I output the value of an enum class in C++11? In C++03 it's like this: #include using namespace std; enum A { a = 1, b = 69, c= 666 }; int main () { A a = A::c; cout << a << endl; } in c++0x this code doesn't…
Adi
  • 2,011
  • 2
  • 15
  • 14
139
votes
12 answers

C++ templates Turing-complete?

I'm told that the template system in C++ is Turing-complete at compile time. This is mentioned in this post and also on wikipedia. Can you provide a nontrivial example of a computation that exploits this property? Is this fact useful in practice?
Federico A. Ramponi
  • 46,145
  • 29
  • 109
  • 133
139
votes
13 answers

How can I echo HTML in PHP?

I want to conditionally output HTML to generate a page, so what's the easiest way to echo multiline snippets of HTML in PHP 4+? Would I need to use a template framework like Smarty? echo '', "\n"; // I'm sure there's a better way! echo…
Robin Rodricks
  • 110,798
  • 141
  • 398
  • 607
138
votes
24 answers

How to easily map c++ enums to strings

I have a bunch of enum types in some library header files that I'm using, and I want to have a way of converting enum values to user strings - and vice-versa. RTTI won't do it for me, because the 'user strings' need to be a bit more readable than…
Roddy
  • 66,617
  • 42
  • 165
  • 277