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

where to put the templatetags directory #34

Closed
Tobi-De opened this issue Sep 26, 2022 · 8 comments
Closed

where to put the templatetags directory #34

Tobi-De opened this issue Sep 26, 2022 · 8 comments

Comments

@Tobi-De
Copy link
Contributor

Tobi-De commented Sep 26, 2022

Hi @adamghill, hope you are doing great.

I'm trying to register custom template tags and I have no idea where to put the templatetags directory, I tried to put in the directory configure as my BASE_DIR but I'm getting this:

django.template.exceptions.TemplateSyntaxError: Invalid filter: 'test'

I tried with the test filter from the docs.

@adamghill
Copy link
Owner

I have a custom template tag here in the example project: example_standalone/templatetags/custom_tags.py. Does that work when you start up the sample project?

@Tobi-De
Copy link
Contributor Author

Tobi-De commented Sep 26, 2022

I have a custom template tag here in the example project: example_standalone/templatetags/custom_tags.py. Does that work when you start up the sample project?

Yes, I just tried running the standalone examle project and the custom tags works.
I'm probably doing something wrong, just to be sure, the name of the custom_tags.py file doesn't matter right?
Are custom tags found relative to the BASE_DIR folder ?
In my case coltrane is intregrated to an existing django project, I use it to render my markdown

@adamghill
Copy link
Owner

just to be sure, the name of the custom_tags.py file doesn't matter right?

Nope, the name custom_tags.py doesn't matter, the only thing that matters is that the module is in the templatetags folder.

In my case coltrane is intregrated to an existing django project, I use it to render my markdown

Ah! Ok, coltrane won't automatically find the templatetags in this case. But, as long as your templatetags directory is in a folder (aka app) which is in INSTALLED_APPS in your project it should work. You will also need to load in the template tag module like normal because coltrane can't automatically load it.

index.md
{% load your_tag_here %}

- this is markdown {% current_data %}
# some_installed_app.templatetags.your_tag_here.py

def current_data():
    # pretend this is a template tag
    pass

Would it be helpful if I create an example_integrated project as well? It would also validate that this works as I expect it does!

@Tobi-De
Copy link
Contributor Author

Tobi-De commented Sep 27, 2022

just to be sure, the name of the custom_tags.py file doesn't matter right?

Nope, the name custom_tags.py doesn't matter, the only thing that matters is that the module is in the templatetags folder.

In my case coltrane is intregrated to an existing django project, I use it to render my markdown

Ah! Ok, coltrane won't automatically find the templatetags in this case. But, as long as your templatetags directory is in a folder (aka app) which is in INSTALLED_APPS in your project it should work. You will also need to load in the template tag module like normal because coltrane can't automatically load it.

index.md
{% load your_tag_here %}

- this is markdown {% current_data %}
# some_installed_app.templatetags.your_tag_here.py

def current_data():
    # pretend this is a template tag
    pass

Would it be helpful if I create an example_integrated project as well? It would also validate that this works as I expect it does!

Ok thanks.

It could be useful to have an example_integrated projet but just a note section in the docs on template tags should be enough I think, most people will probably use Coltrane for it main purpose anyway, a static site generator, I know how template tags works in Django but I thought Coltrane had some special code to load it from a specific folder.

@adamghill
Copy link
Owner

I thought Coltrane had some special code to load it from a specific folder.

template_dir = base_dir / "templates"
templatetags_dir = base_dir / "templatetags"
template_tags = []
if templatetags_dir.exists():
for template_tag_path in templatetags_dir.rglob("*.py"):
if template_tag_path.is_file():
try:
module_name = _get_template_tag_module_name(
base_dir, template_tag_path
)
template_tags.append(module_name)
except InvalidTemplateLibrary as e:
logger.exception(e)
builtins = [
"django.contrib.humanize.templatetags.humanize",
"django.templatetags.static",
"coltrane.templatetags.coltrane_tags",
]
builtins.extend(template_tags)
is how I grab templatetags in standalone or static site mode, but it's all related to default settings which doesn't apply to integrated. I'll make sure to add a note to the docs to hopefully make this more clear.

Let me know if it would be useful to add a feature around this, though! Or if there is a way to format the docs to make it more clear what to do in the different "modes" since I don't think it's very intuitive right now.

I'm going to close this, but feel free to re-open if you run into any issues.

@Tobi-De
Copy link
Contributor Author

Tobi-De commented Sep 27, 2022

I thought Coltrane had some special code to load it from a specific folder.

template_dir = base_dir / "templates"
templatetags_dir = base_dir / "templatetags"
template_tags = []
if templatetags_dir.exists():
for template_tag_path in templatetags_dir.rglob("*.py"):
if template_tag_path.is_file():
try:
module_name = _get_template_tag_module_name(
base_dir, template_tag_path
)
template_tags.append(module_name)
except InvalidTemplateLibrary as e:
logger.exception(e)
builtins = [
"django.contrib.humanize.templatetags.humanize",
"django.templatetags.static",
"coltrane.templatetags.coltrane_tags",
]
builtins.extend(template_tags)

is how I grab templatetags in standalone or static site mode, but it's all related to default settings which doesn't apply to integrated. I'll make sure to add a note to the docs to hopefully make this more clear.

Let me know if it would be useful to add a feature around this, though! Or if there is a way to format the docs to make it more clear what to do in the different "modes" since I don't think it's very intuitive right now.

I'm going to close this, but feel free to re-open if you run into any issues.

For the documentation, I don't have a specific idea in mind at the moment, I think the current layout is already very good.
I don't think a feature on this is needed, the template tags in Django are already pretty straighforward, just a few notes here and there should be enough.

@adamghill
Copy link
Owner

@all-contributors please add @Tobi-De for tests and code

@allcontributors
Copy link
Contributor

@adamghill

I've put up a pull request to add @Tobi-De! 🎉

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

No branches or pull requests

2 participants