Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

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

ralsina
Copy link
Member

@ralsina 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 Idea to avoid repeating ourselves so much in templates WIP Idea to avoid repeating ourselves so much in templates Sep 4, 2015
@Kwpolska
Copy link
Member

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?

@ralsina
Copy link
Member Author

ralsina commented Sep 5, 2015

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

@ralsina
Copy link
Member Author

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.

@ralsina
Copy link
Member Author

ralsina commented Sep 7, 2015

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

@da2x
Copy link
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.

@ralsina
Copy link
Member Author

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 a commit that referenced this pull request Sep 8, 2015
Avoid repeating ourselves so much in templates
@ralsina ralsina merged commit a6cc45c into master Sep 8, 2015
@ralsina ralsina deleted the idea-for-1814 branch September 8, 2015 16:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants