Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

WIP Idea to avoid repeating ourselves so much in templates #2025

Merged
merged 16 commits into from Sep 8, 2015

Conversation

Projects
None yet
3 participants
Owner

ralsina commented Sep 4, 2015

  1. add themes PARENT folders to template lookup
  2. Use inherits parent-theme/templates/whatever.tmpl in whatever.tmpl
  3. If the template already inherits, it can namespace it and wrap trivial calls
  4. Voilá, no need to repeat all of the parent theme's whatever.tmpl in the child theme.

This branch shows the simplified base_helper.tmpl in bootstrap3

You only have to do the silly redefinition-calling-parent for functions that are called from within that same file.

Problems:

  • jinjify needs to catch the reference to parent theme "base/" in this case and patch it to "base-jinja/"
  • In internationalized sites the link to the translations is styled wrong, no idea why
  • jinjified themes should build

Looks like I have run into something where mako is just too different from jinja to make jinjify work :-(

There seems to be no way to make this work in Jinja2, because blocks are not callable!

{% import 'base-jinja/templates/post.tmpl' as basepost with context %}
{% extends 'base.tmpl' %}

{% block extra_head %}
{{ basepost.extra_head() }}
{% endblock %}

So, this means we should use macros instead of blocks, which means defs instead of blocks in Mako. May still be worth doing...

@ralsina ralsina changed the title from Idea to avoid repeating ourselves so much in templates to WIP Idea to avoid repeating ourselves so much in templates Sep 4, 2015

ralsina added some commits Sep 5, 2015

Owner

Kwpolska commented Sep 5, 2015

In internationalized sites the link to the translations is styled wrong, no idea why

Maybe there was some special styling that is now missing?

Owner

ralsina commented Sep 5, 2015

@Kwpolska yes, probably. If we decide this line of work is worth pursuing, I will surely fix that.

ralsina added some commits Sep 7, 2015

Owner

ralsina commented Sep 7, 2015

Apparently this will not work properly outside Mako and/or making it work right with Mako and Jinja means poking most of the templates in complex ways.

Owner

ralsina commented Sep 7, 2015

Oh, well, it works for def-heavy templates, that's much more than nothing :-)

Contributor

da2x commented Sep 7, 2015

What do you mean "def-heavy"? Would this be more useful if there were more defs in the base theme? I'm kind of planning of making more defs in the base theme, if that makes a difference. In a few weeks time, most likely. Testing out some stuff with my own site and thought I'd clean it up and introduce base2 in some weeks time.

Owner

ralsina commented Sep 7, 2015

@Aeyoun for files that are a bunch of defs this is very useful (see the change in nikola/data/themes/bootstrap3/templates/base_helper.tmpl), basically, only have to keep the functions you change, the rest will be "inherited".

For blocks, that doesn't work, so this technique doesn't help avoid repetition (fairly: it works in Mako, doesn't work in Jinja).

So, the more templates we have that are collections of defs, the more this avoids stupid repetition.

ralsina added some commits Sep 7, 2015

ralsina added a commit that referenced this pull request Sep 8, 2015

Merge pull request #2025 from getnikola/idea-for-1814
Avoid repeating ourselves so much in templates

@ralsina ralsina merged commit a6cc45c into master Sep 8, 2015

1 of 3 checks passed

continuous-integration/travis-ci/pr The Travis CI build failed
Details
continuous-integration/travis-ci/push The Travis CI build failed
Details
continuous-integration/appveyor AppVeyor build succeeded
Details

@ralsina ralsina deleted the idea-for-1814 branch Sep 8, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment