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

[Question] Inject content after template render #40

Open
nd2s opened this issue Dec 3, 2017 · 1 comment
Open

[Question] Inject content after template render #40

nd2s opened this issue Dec 3, 2017 · 1 comment
Labels

Comments

@nd2s
Copy link

nd2s commented Dec 3, 2017

Currently I am embedding SVG icons by calling a function {%= p.SVGSymbol("svg/icon/close.svg") %} that creates a SVG <use xlink> tag and remembers the symbol being used. And at the end of the template I embed the automatically generated SVG definitions (so that only icons that are actually used are embedded).

I know need to insert the sprite at the beginning of the content - before I know what icons I need to embed.

What would be the best way to achieve that without having to render all templates twice for every call?

@valyala
Copy link
Owner

valyala commented Aug 24, 2018

Just split the original template func into two template funcs:

  1. The first one - p.PageContents - calls p.SVGSymbol, which remembers the content required for the embedded SVG inside p.
  2. The second one - p.SVGDefinitions - generates SVG definitions for the remembered SVG symbols from the first template.

Then the code would look like:

pageContents := p.PageContents()
svgDefinitions := p.SVGDefinitions()
resultingText := svgDefinitions + pageContents

This code isn't optimal from memory allocations point of view, but it calls p.PageContents only once.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants