Skip to content

Global template variables? #144

Closed

Description

@heathermiller

This is more of a question (and potentially a feature request if this isn't supported) than an issue – is there some way to declare global variables?

For example, in my config.yml, I'd like to define some global variable, version-num that, of course, represents the current version of the library that I'm documenting. I'd naturally like to use this all over in my documentation and have only one place to update the version number when releasing a new version – in config.yml.

Though I've tried to do this and beyond the pre-defined variables in the mkdocs documentation, nothing that I declare seems to be picked up.

Is there some way to do this that I don't see?

PS, Jekyll allows for this, so I'm basically assuming the same semantics as Jekyll, i.e., that user-defined variables in config.yml are globally accessible

Activity

  1. jimporter commented on Sep 30, 2014

    @jimporter
    Contributor

    I know it used to be possible to set custom variables in the config file and use them in templates, but I think that was removed a while back. Custom variables that only templates can use aren't super-useful though.

    I think mkdocs recently added support for using Markdown extensions. Maybe there's an extension that does something like what you want? https://pythonhosted.org/Markdown/extensions/

  2. heathermiller commented on Sep 30, 2014

    @heathermiller
    Author

    Thanks for your response.

    There's a "meta data" plugin that'd make it so I'd have to manually add meta data to each individual markdown file, which defeats the purpose of globally defining something like a project title or version number. So, upon first glance, no, it doesn't seem like there's an extension I can use.

    Custom variables that only templates can use aren't super-useful though.

    I don't understand how this can be true. Could you explain what you mean? I provided one very useful example especially if you're documenting a library – version numbers for the library that I'm documenting. I'd much rather write {{ site.version-number }} in one place than have to grep all of my documentation to update version numbers upon each release. It's useful both in templates (it's conceivable that I'd like to have the version number of my library a few places in my template) and markdown documentation files.

    So I attest that custom variables that templates + users can use are super useful. Jekyll is excellent validation of this.

    I've lead the documentation of the entire Scala programming language using Jekyll (we were looking to mkdocs because it'd allow us to include docs in our build a bit easier than Jekyll), and I promise you that having the ability to define custom, globally accessible variables in config.yml is useful for real, big project documentation.

  3. jimporter commented on Sep 30, 2014

    @jimporter
    Contributor

    It's useful both in templates (it's conceivable that I'd like to have the version number of my library a few places in my template) and markdown documentation files.

    Right, but the previous implementation of this only supported it in the template, and not the Markdown files, which makes it a lot less useful (unless you want to write your own custom theme, which I think is overkill for showing a version number in the appropriate spots; and even then, it wouldn't be usable in the body of the docs).

    To be clear, I'm not saying your suggestion is invalid; it's totally valid. I'm just saying that the old implementation of custom globals in MkDocs wasn't very useful.

  4. heathermiller commented on Sep 30, 2014

    @heathermiller
    Author

    Just to make sure this point isn't lost in the quick reply.

    It's useful both in templates (it's conceivable that I'd like to have the version number of my library a few places in my template)

    I think my statement right there is validation that it is useful. There's more than a version number that I'd maybe like to define globally. For example, a list of contributor names, project title, tagline, an email address, social media URLs.

    So to reiterate once again – I think it is useful in the template as well.

  5. heathermiller commented on Sep 30, 2014

    @heathermiller
    Author

    And PS, I've already written my own theme. I still can't use any custom globally defined variable in it.

  6. jimporter commented on Sep 30, 2014

    @jimporter
    Contributor

    Well, if you just want global variables for a theme without forwarding to the Markdown files, then you want any commit before 7167406. It should be pretty straightforward to undo the relevant part of that commit on master (you'd just need to undo this line), but for it to be more generally useful, I think you'd want to forward the variable along to the Markdown files.

    Really, I think part of the issue is that the set of predefined global variables is rather sparse to begin with...

  7. jimporter commented on Sep 30, 2014

    @jimporter
    Contributor

    Also, I suppose it should be noted that I'm mainly just a user of MkDocs, and as such I don't have any real say in anything here, but I think your suggestion would be pretty useful, provided the variables could be referenced in the Markdown files as well as the templates.

  8. d0ugal commented on Oct 1, 2014

    @d0ugal
    Contributor

    I think given @tomchristie removed this ability recently we probably need to get some input from him on this. Otherwise the use case does seem reasonable to me.

  9. ACXgit commented on Oct 22, 2014

    @ACXgit

    +1 for global template variables

  10. lovelydinosaur commented on Oct 23, 2014

    @lovelydinosaur
    Collaborator

    Okay, so issue here is that if we simply pass the entire config through to the context then it becomes unclear what variables are user-defined, and what are built-in.

    Options:

    • Not worry about that, just do it anyway. Probably pass the config in as config so that it is namespaced.
    • Use custom_<my_variable> names or nested custom: to pass through custom variables.

    This...

    custom:
        my_custom_variable: "Blah"
        another_one: 123
    

    Passed through to the templates as custom seems like it might be a good option?

  11. d0ugal commented on Oct 23, 2014

    @d0ugal
    Contributor

    +1 to the last option, a key/value mapping of custom data. That sounds like a good approach to me but I'm not sure about the name custom - it doesn't strike me as self explanitory. The one I'd use naturally is context but that is due to a Jinja2/Django background so I don't think it makes sense here.

    Something like template-variables might make sense, but it gets quite verbose.

  12. d0ugal commented on Oct 23, 2014

    @d0ugal
    Contributor

    As @heathermiller mentioned Jekyll has a similar feature, I had a look to see how they do it - http://jekyllrb.com/docs/datafiles/

    Probably overkill for us, but maybe worth considering.

  13. 5 remaining items

  14. modified the milestones: 0.13.0, 1.0.0 on Apr 24, 2015
  15. waylan commented on May 6, 2015

    @waylan
    Contributor

    Reading through this PR, it occurs to me that the original feature request assumes that the user can define any custom settings (including settings that mkdocs knows nothing about) and that those should then be available in the template. However, the work in #496 makes that impossible as only "known" settings will validate. Creating an "unknown" setting would result in an error. In that case, a custom setting which contains a dict of user defined keys and values makes sense. Something like custom, extra, or user_defined seems logical (I like extra). Then, that setting can be passed to the template regardless of whether all of the config is.

    I suppose the real question is: Is this issue about the custom user-defined settings or all settings being available in the template. If it is the former, then that should be an easy addition to #496 and the rest can be punted for later consideration. However, if it is the later, a final decision needs to be made first.

    For the record, I don't understand the aversion to having all of the config available to the templates. For Markdown files, I agree that not everything should be available (if #304 ever gets implemented), but whats the big deal for templates?

  16. d0ugal commented on May 7, 2015

    @d0ugal
    Contributor

    @waylan

    For the record, I don't understand the aversion to having all of the config available to the templates. For Markdown files, I agree that not everything should be available (if #304 ever gets implemented), but whats the big deal for templates?

    I'm not sure anyone is against it really, just trying to make the most sensible choice. I think it would make sense to pass the full config.

    I'd like to look at this after I finish the work in #496. I think my plan will be to make the full config available as {{ config.setting_name }} to templates. Then use extra to have a dictionary of whatever the user wants. Then it could be accessed with {{ config.extra.my_custom_thing }}.

    Over time I'd like to remove the duplication with settings in the template context, and have them all accessible via {{ config }} - that would probably be a switch for a 1.0 release.

  17. added a commit that references this issue on May 9, 2015
    cf10026
  18. lovelydinosaur commented on May 11, 2015

    @lovelydinosaur
    Collaborator

    Wrote this before I saw @d0ugal's fix, but posting it for clarity all the same. The fix in #510 handles all this properly, similar to the second of the options presented below.

    For the record, I don't understand the aversion to having all of the config available to the templates.

    There's two acceptable options here...

    • Have the entire config available, and namespaced under {{ config.<setting> }}.
    • Have a config key for user defined / theme specific settings {{ config.context.<setting> }}.

    The first is nice and simple.
    The second has the advantage that it's explicit which of the config variables are built in MkDocs settings, and which are just extra template context, but don't have any behavior affect in the code.

  19. d0ugal commented on May 11, 2015

    @d0ugal
    Contributor

    @tomchristie Just for clarity, the change I made means that {{ context.<setting> }} is the current way to access user defined globals. Although, I went for extra in the end. So it is {{ extra.<setting> }}.

    I have been thinking about this further and I think this actually makes more sense: {{ config.extra.<setting> }} as it is still part of the config after all. Then users can access anything in the config and I we can remove a bunch of code duplication and documentation duplication.

  20. lovelydinosaur commented on May 11, 2015

    @lovelydinosaur
    Collaborator

    @d0ugal Yes, perfect. We may even want to move some non-behavioral stuff such as copyright and google_analytics into the extra block. It'll help the maintainance of the project if we can reduce the number of valid settings keys - less moving parts then, and the context stuff all nicely separated out.

  21. reopened this on May 11, 2015
  22. d0ugal commented on May 11, 2015

    @d0ugal
    Contributor

    @tomchristie Yup, I would like to move those too.

    I'm not sure how to do the transition, it might be that we can duplicate google_analytics etc. to the old names with some sort of lazy-property-function that logs a warning if it is accessed. Just removing them wouldn't be great, as Jinja2 wont even complain, unless you try to access a property on that object (which you typically wouldn't for google_analytics or copyright).

  23. lovelydinosaur commented on May 11, 2015

    @lovelydinosaur
    Collaborator

    Could we have a transition period where the config loading checks explicitly for them, raises a warning if they exist, and duplicates them into extra if so?

  24. d0ugal commented on May 11, 2015

    @d0ugal
    Contributor

    Oh, of course, that makes sense. None of these have useful defaults so they will always be in the config if they are used. Much easier.

  25. d0ugal commented on May 24, 2015

    @d0ugal
    Contributor

    This is done, moving the discussion about deprecating/moving other config options to #551

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions