Jump to content

Is a content-type header required when using a Twig template?


NotionCommotion

Recommended Posts

Or should the Twig class be used to generate the content type header?

require_once '../../../vendor/autoload.php';
Twig_Autoloader::register();
$loader = new Twig_Loader_Filesystem('templates');
$twig = new Twig_Environment($loader, array('debug' => true,));
$twig->addExtension(new Twig_Extension_Debug());
header('Content-type: text/html; charset=utf-8');
echo $twig->render('myTemplate.html', array());
Link to comment
Share on other sites

How do you want to generate the header with Twig? I'm not aware of any such feature.

 

Personally, I define the encoding globally in the webserver configuration. Since I almost always use UTF-8, it wouldn't make sense to repeat the declaration per script. In the rare cases where I do need a different encoding, I can still override the defaults.

Link to comment
Share on other sites

You should always declare the encoding in the Content-Type header....

 

Your advice makes sense, however, I might have misunderstood your earlier post.  I heard that I should always explicitly define the charset when setting a Content-type header.  This seems contradictory to setting it globally in the webserver configuration.

Link to comment
Share on other sites

If you declare the encoding at webserver level, that's just as explicit as emitting a Content-Type header in every single script. But the global declaration is clearly more convenient (and easier to change in case that's necessary).

 

Where you set the encoding isn't really important. It's only important that you set it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.