Django - How to break the HTML code in file - django

I've many lines of code in HTML(over 1000), I want to break the HTML code into different files and then include that code in the main file. In PHP, we have <?php
require('somefile.php');
?>, and we can include the somfile.php under main file.
Is there something similar in Django?

You can try "include",
{% include xxxx.html %}
More details is in django's document

Related

How to deploy pure html to Octopress?

I am using Octopress to write posts, which uses markdown file to generate html files, using :
rake new_post['my_post']
rake generate
But what if I need to add some JavaScript demo inside my post, which I need to write some code inside the post, which may possibly be a html page I am writing as.
Can I achieve this with Octopress and remain overall consistency of style?
You can put your Javascript block into its own HTML file, in your source/_includes/ directory. Then you can embed that into your post using Liquid include tags:
---
layout: post
title: "JS Demo"
date: 2015-01-01 01:01:01
categories:
---
{% include myjs.html %}
and the contents of myjs.html would be:
<div id="myelement"></div>
<script>
$('div#myelement').text("hello world");
</script>
and myjs.html would be at source/_includes/myjs.html. Then your final page source code would (for example) render as:
<div><h1>JS Demo</h1></div>
<div id="myelement">hello world</div>
If you want to structure the Javascript code you're including a bit more, you can make a directory for Javascript files in (e.g.) source/_includes/demo/, then put your Javascript into source/_includes/demo.html. Then your markdown would have the following Liquid include tags:
{% include demo/demo.html %}

Django include template url

I have a template problem.
I have localizated form so, for example, in all italians pages I have the same form and I'm trying to include that instead repeat the same code over and over in the pages.
In
/category1/IT/template.html
I have to include the
/form/it.html
If I use
{% include "form/it.html" %}
the block defined in the it.html is like empty and I see the default [english] form instead the it one.
If I try to write
{% include "../../form/it.html" %}
django tells me that the template does not exixt..
If I cut the it.html code in the IT/template.html this work.
Can someone help me to figure why the blocks in the included file do not work?

How to put translatable text (including English) into files in Django

I'd like to have all of my templates' actual non-html text in one (or multiple) seperate files in Django. At the moment my templates are quite jam-packed by passages like:
{% if request.session.lang == "en" %}
Some text in English
{% else %}
Some text in the default language
{% endif %}
The templates' text (main language or English) gets changed often by other people, so I would like to just have some files, which other people can edit as well (without having to edit the actual view-files).
After reading the localization section of django docs, it seems that one still has to hardcode text (English in the docs' examples) into the templates/views.
Example from django docs on generated .po files:
msgid "Welcome to my site."
msgstr ""
I'd rather have something like:
msgid APP-XY_VIEW-XY_INTRODUCTION
msgstr ""
Of course, the obvious solution seems like using something like:
ugettext('APP-XY_VIEW-XY_INTRODUCTION') # in a view
However, I'd like to make sure if there's no other solution (without creating some custom id string literals, that are hardcoded in every view/template).
Thanks very much!
/edit, Django Version 1.4.5
You don't say what version of Django you are using (you should pretty much always include this information - it will help you get the best answers). But, you should just be able to put
{% load i18n %}
at the top of your template. Then you can just call trans and handle it like you would your models, etc.
<title>{% trans "My very important title" %}</title>
The Django book 2.0 has a pretty good chapter on this topic. Might be work a read? Click here for more info.
You can create language files for the same language as your project and it will override the hardcoded strings (both template and views)
my project:
LANGUAGE_CODE = 'en-US'
In locale/en/LC_MESSAGES/django.po my translators can override my faulty/bad choice of words

django templates - include another block from same folder

I'm new to django, I'm trying to figure out how to include other html files from the same subfolder to my main template file.
I've tried the following to no avail:
{% include './_shared.html' %}
I'll eventually have many template folders. Can someone help me out?
You have to put your sub folders inside your template directory
You have to define your template path in your settings.py
now you can include that template file like {% include '/yoursubfolder/file.html' %}
Try this:
{% include request.path+"_shared.html" %}
You might have to add slash between path and file name.

How do I include raw HTML files in Symfony2/Twig templates?

I'm working on a project in Symfony2 and I have several small pieces of html that need to be included in one of my main views. According to the official Twig documentation I should be able to simply use {% include 'filename.html' %} but in Symfony unless the filename ends in ".html.twig" it throws and error saying it cannot find the file. I'd like to avoid using Twig templates for these files since they have no dynamic content and lots of double braces (they're javascript templates) and requiring the template designer to have to wrap every one of these files in {% raw %} tags seems like a really Kludgey way to do it.
I also came upon the same problem trying to find a solution to include files (mustache templates) as raw in Twig templates so Twig doesn't try to parse them.
At first I had my mustache template files named simply sometemplate.html and wrapped in {% raw %} tags. This worked for a while, but then I started using PhpStorm IDE with the Handlebars plugin (for mustache syntax). For PhpStorm to recognize the files as mustache syntax, they need to have a unique file extension (.mustache by default), so I renamed my sometemplate.html to sometemplate.mustache but I really disliked the idea that my mustache templates needed to be wrapped with Twig tags. So I ended up doing what #rdjs said in his option 3. This is the best solution imo.
Here's the working Twig extension function I made:
function twig_include_raw(Twig_Environment $env, $template) {
return $env->getLoader()->getSource($template);
}
$twig->addFunction('include_raw', new Twig_Function_Function('twig_include_raw', array('needs_environment' => true)));
With this in place you can easily include files as "raw" without Twig parsing them by doing:
{{ include_raw('sometemplate.mustache')|raw }}
I even made a Twig macro for simplifying including mustache templates to HTML head sections:
{% macro mustache_script(id, file) -%}
<script id="{{ id }}" type="text/x-mustache-template">
{{ include_raw(file)|raw }}
</script>
{%- endmacro %}
And after importing the file with the above macro to your Twig template ({% import "macros.twig" %} for example), you can easily import mustache template files in your Twig templates by simply doing {{ mustache_script('sometemplate_tpl', 'sometemplate.mustache') }} inside a HTML <head> section.
I hope this helps someone who's looking for a solution to the same problem.
A quick recap on twig file extensions (taken from the documentation):
Every template name also has two extensions that specify the format and engine for that template.
AcmeBlogBundle:Blog:index.html.twig - HTML format, Twig engine
AcmeBlogBundle:Blog:index.html.php - HTML format, PHP engine
AcmeBlogBundle:Blog:index.css.twig - CSS format, Twig engine
By default, any Symfony2 template can be written in either Twig or PHP, and the last part of the extension (e.g. .twig or .php) specifies which of these two engines should be used. The first part of the extension, (e.g. .html, .css, etc) is the final format that the template will generate.
Therefore it makes sense to me that including a file as .html would be at the least ambiguous even if it didn't throw an error.
So you have 3 choices:
If the files are purely javascript then include them as script tags in your page.
If they are mixed HTML and JS then escape the JS with {% raw %} and include the files as foo.html.twig templates. If there are lots of scripts being included like this then most likely your designers could do with a little refactoring and move the bulk of their scripts to external files (see option 1)
If you really insist you could always write a Twig extension to include raw HTML files. (EDIT: See #Haprog's answer below for more details on this option).
{{ include_html('foo/bar.html') }}
UPDATE 2015 twig has since added the source function:
{{ source('AcmeSomeBundle:Default:somefile.html.twig') }}
Kudos to #Nigel Angel in the comments below for option 4.
I came accross this post, as I had a similar question. After an hour or so searching and trying, I found out, that as from Twig Version 1.15 the "source Function" was added.
Maybe that helps someone in the future.
Follow up on Kari, if you're in an extension.. you can apply it this way.
public function getFunctions() {
return [
'include_raw' => new \Twig_Function_Method($this, 'twig_include_raw', array('needs_environment'=> true, 'is_safe'=> array('all')))
];
}
And that'd be a $this->twig_include_raw type method. You'd include within your template as:
{{ include_raw("my/file/here.html.twig") }}
No need for " | raw".