Comments (6)
Hi @agrahl , thanks for opening the issue. About the fragments:
why it is not possible to use the "include" expression with a dynamic value
When a template is prepared, a user of Stencil may need to be able to list the names of all the fragments the template requires. Think of it like enumerating the "dependencies" of a template so we could tell in advance if all the dependencies are available before actually rendering the template. See: preparedTemplate.getVariables().getAllFragmentNames()
. If the template names can be dynamic values, there would be no way of enumerating all the possible values.
And it would make the templates more readable, because else you need to have a long list of "if" expressions.
This is a fair point. Perhaps we could be more permissive with the {% include
directives and also allow dynamic expressions, and getAllFragmentNames()
would return only the string values. I will check back on this issue around middle of this week to see what it would require to implement this functionality.
I also played around with the html function, but here it is not enough. The supports of formatting options is very small.
Please feel free to open a separate Issue with feedback on what you were missing.
from stencil.
Hi @erdos ,
thanks for the fast reply. Perhaps instead of an entry of PreparedFragment in the Fragments-Map, we could register a PreparedFragmentFunction which allows to select a fragment on basis of parameters (similar to the Function interface). It would then return a concrete PreparedFragment.getImpl() as return object that can be handled as usually.
{%=include myfragment(....)%}
=> dynamic select a fragment
{%=include "myfragment"%}
=> static select a fragment
Perhaps the change would be very small if we could use the Provided Function interface directly for this. The implemented Function would then internally directly return the value from the selected PreparedFragment.getImpl().
public interface PreparedFragmentFunction { ... Object call(Object... arguments) throws IllegalArgumentException; ... }
And a concrete implementation could look like this.
public Object call(Object... arguments) throws IllegalArgumentException { return this.myFragments.getOrDefault(arguments[0], defaultFragment).getImpl(); }
Of course here also more logic is possible, there is no limit here :-)
The function PreparedTemplate.getVariables().getAllFragmentNames() would return in that case also only the name of the used function in the list (the name of the outer function, so when as parameters also functions are used that does not matter).
These are only first ideas. As I said I have not so much understanding of the underlaying Closure implementation. So I have difficulties to have a feeling on the impact of the Closure implementation.
Regards
Alexander
from stencil.
Change was released in v0.5.7, closing issue.
from stencil.
Hello @erdostw,
thanks for the change. Do I understand it correctly, that we still need to load all fragments in the fragment map and give that to the API.render(...)
method.
After this we can set the fragment to load via {%=include expression%}
the expression must evaluate to a String that is the name of a fragment contained in the fragment map.
Is that correct, or can the expression also evaluate to a PreparedFragment object?
Regards
Alexander
from stencil.
Hey @agrahl , for now, the dynamic fragment only works if the expression evaluates to a String which is the name of the fragment.
The reason behind this idecision that preparing templates/fragments has a visible cost of unpacking and analysing the template file. The templates/fragments also have to be cleaned up after last usage because they use space in the temp directory. Therefore, it is advised to precompute and cache fragments in advance before rendering.
from stencil.
Hi @erdostw,
that with the parsing and preparing the fragments is clear. Also the clean up, if we shutdown the application or redeploy the templates. That is also why I asked, if we could have here something that also allows to return a PreparedFragment object. That would make it easier to just prepare the stuff that is needed in the template. You could imagine this like a data driven selection of segments in a template. Each segment is a PreparedFragment. Depending on the input data and the template that uses it for rendering, it will select different segments (PreparedFragments) to be included. Because it depends on the input data, I would avoid to make a data analysis before the rendering via the template is called. This would split up the implementation of a template and would make it more difficult to maintain.
You also made the change with the temporary directory (thanks for this too), it is now easy to find all resources that belong to an application or even a running deployment of templates. So I would not have fear, that we would run here into problems.
Regards
Alexander
from stencil.
Related Issues (20)
- Can't subtract from result of coalesce expression HOT 2
- Newline character support HOT 4
- Calculating a range between dates HOT 3
- add functions to service on-the-fly HOT 2
- Scaling stencil: support fetching templates from an object storage
- Define local variables in the Tempalte HOT 2
- `map` function doesn't work HOT 2
- Dividing by dynamic value doesn't work HOT 2
- Page breaks HOT 8
- function to insert page break
- Loop over collection with an index HOT 1
- Formatting for integer division breaks in some cases HOT 2
- Sum/Map with conditions HOT 1
- Add support for replaceAll HOT 2
- For loops cause issue with numbered bullet points HOT 5
- Is there a chance for more meaningful errors in Stencil being implemented? HOT 10
- Getting stackoverflow exception during parsing of template regarding whitespace parsing HOT 5
- Allow to set working directory to overwrite usage of system property java.io.tmpdir HOT 4
- Rendered pptx file is considered corrupted by MS PowerPoint HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from stencil.