You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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.
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.
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.
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.
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...
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.
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.
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.
+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.
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?
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.
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.
@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.
@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.
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).
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?
Activity
jimporter commentedon Sep 30, 2014
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/
heathermiller commentedon Sep 30, 2014
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.
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.
jimporter commentedon Sep 30, 2014
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.
heathermiller commentedon Sep 30, 2014
Just to make sure this point isn't lost in the quick reply.
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.
heathermiller commentedon Sep 30, 2014
And PS, I've already written my own theme. I still can't use any custom globally defined variable in it.
jimporter commentedon Sep 30, 2014
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...
jimporter commentedon Sep 30, 2014
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.
d0ugal commentedon Oct 1, 2014
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.
ACXgit commentedon Oct 22, 2014
+1 for global template variables
lovelydinosaur commentedon Oct 23, 2014
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:
configso that it is namespaced.custom_<my_variable>names or nestedcustom:to pass through custom variables.This...
Passed through to the templates as
customseems like it might be a good option?d0ugal commentedon Oct 23, 2014
+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 iscontextbut that is due to a Jinja2/Django background so I don't think it makes sense here.Something like
template-variablesmight make sense, but it gets quite verbose.d0ugal commentedon Oct 23, 2014
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.
5 remaining items
waylan commentedon May 6, 2015
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, oruser_definedseems logical (I likeextra). 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?
d0ugal commentedon May 7, 2015
@waylan
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 useextrato 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 a1.0release.Add `extra` config to support global template vars
lovelydinosaur commentedon May 11, 2015
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.
There's two acceptable options here...
{{ config.<setting> }}.{{ 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.
d0ugal commentedon May 11, 2015
@tomchristie Just for clarity, the change I made means that
{{ context.<setting> }}is the current way to access user defined globals. Although, I went forextrain 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.lovelydinosaur commentedon May 11, 2015
@d0ugal Yes, perfect. We may even want to move some non-behavioral stuff such as
copyrightandgoogle_analyticsinto theextrablock. 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.d0ugal commentedon May 11, 2015
@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_analyticsetc. 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 forgoogle_analyticsorcopyright).lovelydinosaur commentedon May 11, 2015
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?
d0ugal commentedon May 11, 2015
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.
d0ugal commentedon May 24, 2015
This is done, moving the discussion about deprecating/moving other config options to #551