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

tplimpl: Allow text partials in HTML templates #3347

Merged
merged 1 commit into from Apr 16, 2017

Conversation

bep
Copy link
Member

@bep bep commented Apr 15, 2017

Most obvius benefit of this is to include CSS partials with css file suffix into HTML templates.

A valid workaround would be to rename the file mystyles.html, but that doesn't work too good for external editors etc.

The css partial is a method used in some themes before Hugo 0.20, but then it stopped working.

This commit reintroduces that behaviour.

Note that the regular layout lookups for text templates, i.e. "single.json" will be
prefixed with "_text/" on lookup and will only match in the text collection.

Fixes #3273

@bep
Copy link
Member Author

bep commented Apr 15, 2017

I have put #3273 into a 0.20.2 milestone. I notice some breaking themes that include partials on the form "mystyles.css", which does not work without also allowing fetching text templates into html and the other way around.

I think we should just put this out there before people start to make clever workarounds. This will make the inline CSS story so much better.

I will do some more manual testing and thinking about this, but please comment.

And note: This is relevant for partials only.

@bep bep requested a review from n10v April 15, 2017 12:30
@bep bep force-pushed the allow-txt-and-html branch 2 times, most recently from 3fed467 to b15e145 Compare April 15, 2017 12:35
@bep
Copy link
Member Author

bep commented Apr 15, 2017

Just tested this with my site.

So:

<style type="text/css">
         {{ partial "mystyles.css" . }}
</style>
=>
<style type="text/css">
        ZgotmplZ
</style>

<style type="text/css">
          {{ (partial "mystyles.css" .) | safeCSS }}
 </style>

=>

<style type="text/css">
        body {
	    background-color: blue;
        }
</style>

Which I would say is expected and good behaviour and I have added tests for it. I think I will figure a way for the plain text partial variant to return string; then I think this would be "all good".

@bep bep force-pushed the allow-txt-and-html branch 2 times, most recently from afcae00 to 05591bd Compare April 15, 2017 13:54
@bep
Copy link
Member Author

bep commented Apr 15, 2017

I'm talking to myself here, but now I'm happy with the implementation.

Text partials will return a string, so if used in HTML templates where it would normally be auto-escaped, you will have to use the safeHTML or simliar func to mark it as safe.

Most obvius benefit of this is to include CSS partials with css file suffix into HTML templates.

A valid workaround would be to rename the file `mystyles.html`, but that doesn't work too good for external editors etc.

The css partial is  a method used in some themes before Hugo 0.20, but then it stopped working.

This commit reintroduces that behaviour.

Note that the regular layout lookups for text templates, i.e. "single.json" will be
prefixed with "_text/" on lookup and will only match in the text collection.

Fixes gohugoio#3273
@bep bep changed the title tplimpl: Allow mixed HTML and text templates tplimpl: Allow text partials in HTML templates Apr 15, 2017
@rdwatters
Copy link
Contributor

This is a great addition and easier than the hack-ish approach I was taking to CRP with printf. If nothing else, I think long-time Hugo users are used to leveraging safeJS, safeHTML, and safeCSS as a matter of habit with Golang templates. Thanks for this added feature and convenience @bep.

@bep bep merged commit 1cf2920 into gohugoio:master Apr 16, 2017
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow mixed text/html templates
4 participants