Can I have templates for rst files with Sphinx? - jinja2

I have several rST pages that follow the same skeleton, but with different values and paragraphs in some places.
Ideally, I would imagine something like this (simplified example):
page-model.rst
Feature {% block title %}Index{% endblock %}
============================================
{% block description %}
Not available.
{% endblock %}
And then:
page-1.rst
{% extends "page-model.rst" %}
{% block title %}Load{% endblock %}
{% block description %}
Load data from hard disk.
{% endblock %}
Is that possible out of the box?
Should I hack something myself to run Jinja before generating docs? Or define my own directives?
Is that a bad idea? :)
Thanks for your insights!

Related

Unable to extend django template properly?

I have a base.html template which contains basic html definitions .The other templates are defined below
// pages/dashboard.html
{% extends 'base.html' %}
{% block body %}
{% include 'components/nav.html' %}
{% include 'components/dashboard.html' %}
{% endblock %}
// components/dashboard.html
<div class="page-container">
<div class="main-page">
{% block dashboard %}
{% endblock %}
</div>
</div>
// mailer/new.html
{% extends 'pages/dashboard.html' %}
{% block dashboard %}
<h1>hello</h1>
{% endblock %}
The view renders mailer/new.html the problem is that the block containing <h1>hello</h1> is not working. Where am i going wrong with that ?
{% include %} doesn't work that way. See the note in the documentation for that tag:
Blocks are evaluated before they are included. This means that a template that includes blocks from another will contain blocks that have already been evaluated and rendered - not blocks that can be overridden by, for example, an extending template.

Using Jekyll how can I check if file exist in my folder?

I have an _includes folder, and within it I have nav subfolder containing a nav.html file.
I've been trying to check if nav.html exists in nav. If it does, I want to include that nav.html file. If it doesnt exist, there will be a default nav for my template.
Here's the code I have right now:
{% for static_file in site.static_files %}
{% if static_file.path == '/nav.html' %}
{% include nav.html %}
{% endif %}
{% endfor %}
Hope you guys can help me out.
Many thanks in advance
I am not able to find the solution using existing liquid tags so I solved this by using a plugin with a custom liquid tag.
{% capture fileExists %}{% file_exists _includes/nav/custom/nav.html %}{% endcapture %}
{% if fileExists=="true" %}
{% include /nav/custom/nav.html %}
{% else %}
{% include /nav/default/nav.html %}
{% endif %}
See GitHub Repo

Flask-Admin Template

I'm would like to use flask-admin and integrate it in my own layout which is based on flask-bootstrap. I don't care about the navbar that comes with flask-admin would just use the pure list view. I'm struggling to find an elegant solution, such that I don't have to write my own list.html. Structure is:
base.html:
{% extends "bootstrap/base.html" %}
{% block content %}
<div class="container">
{% block page_content %}
{% endblock %}
</div>
{% endblock %}
My normal other templates just extend this base.html and overwrite the page_content block. The idea is to have now my own \admin\master.html which should extend the page_content as well. Something like:
\admin\master.html
{% extends 'base.html' %}
{% block page_content %}
{% block body%} here most of the list.html from flask-admin should appear
{% endblock%}
{% endblock %}
It seems that flask-admin in list.html defines also a body block, which seems to overwrite the body block from the flask-bootstrap template. I had the impression that jijna2 templating is somehow hierarchical. e.g. blocks get filled from the direct extension.
I could easily create my own list.html, edit.html and create.html but would probably duplicate most of the code. Is there a more elegant solution?
I ended copying over everything and creating my own. I found required changes to most of those files to get things to display properly anyways.

Django-Child template-Base.html not rendering

I am using the Django template system in my app, and in the child template I reference the {% extends "base.html" %} accordingly, though when redenring the template, it only comes up with the html tags and no css styling-thus not picking up the base.html template altogether.
Please advise on what I am doing wrong?. Could it be an issue with it not finding the location of my base.html template?
This is the child template code:
{% extends "base.html" %}
{% block content %}
{% for field in form %}
{{ form }}
{% endfor %}
{% endblock %}
This is the base.html template code (relevant parts):
<div id="content">
<h2>
Page heading - This is where the functionality goes...
</h2>
<FIELDSET><INPUT class=Test value="Test" type=submit></FIELDSET>
{% block primary %}{% endblock %}
</div>
Please let me know if you need any firther information to solve this issue.
-I will add the views.py this evening (London time)--
This all all sorted, many thanks for the help
Thanks
Like goliney mentioned...
You need to have a part in your base.html file that has
{% block content %}
{% endblock %}
Then you can override that in your other template that extends the base.html template.
Currently in your child template you could do a
{% block primary %}
whatever you want here and this would show up where you marked on the base.html template
{% endblock %}
Also not sure if you are just putting together a quick example or not. You will need to create the
<form action="" method="post">{% csrf_token %}
for loop here
</form>

Website with unique homepage layout in Django Templates

I am building a website with a unique homepage design (homepage has a different header and logo arrangement than all the other pages). However, I would like to have a base template which everything inherits from, to cut down on redundancies.
-base.html
-basehome.html (inherits from base.html)
-basesecond.html (inherits from base.html)
-about.html (inherits from basesecond.html)
-etc...
So base.html holds the html declaration and the structure. Basehome.html and basesecond.html contain the different header structures and the various other pages on the site inherit from basesecond.html.
So the problem I keep running into is, it seems like I need to put a block within a block to handle the body content which is obviously contained the (furthest) child template. As far as I know, Django does not let you do this.
base.html--
<html>
<head>
<title>Mysite</title>
</head>
<body id="{% block bodyholder %}{% endblock %}">
<div id="hd">{# start of hd #}
{% block hd %}{% endblock %}
</div>{# end of the hd #}
<div id="bd">{# start of body #}
{% block bd %}{% endblock %}
</div>{# end of body #}
</body>
</html>
basehome.html--
{% extends "base.html" %}
{% block bodyholder %}bodyhome{% endblock %}
{% block hd %}
big logo and wide header
{% endblock %}
{% block bd %}
homepage body content
this part works just like I want it to.
{% endblock %}
basesecond.html--
{% extends "base.html" %}
{% block bodyholder %}bodysecond{% endblock %}
{% block hd %}
small logo and narrow header
{% endblock %}
{% block bd %}
second page body content
here is where I want to put extra blocks like
{% block unique about page sidebar %}{% endblock %}
but it breaks the template system
{% endblock %}
What is the best way to solve this problem?
If you're using exactly what you have shown you need to re-write a small portion:
instead of this
{% block unique about page sidebar %}{% endblock %}
replace it with this
{% block unique %}{% endblock %}
{% block about %}{% endblock %}
{% block page %}{% endblock %}
{% block sidebar %}{% endblock %}
Otherwise, everything looks like it should work. What error codes or behavior are you seeing that you aren't expecting?