Giter Site home page Giter Site logo

Comments (6)

erdos avatar erdos commented on June 1, 2024

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.

agrahl avatar agrahl commented on June 1, 2024

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.

erdostw avatar erdostw commented on June 1, 2024

Change was released in v0.5.7, closing issue.

from stencil.

agrahl avatar agrahl commented on June 1, 2024

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.

erdostw avatar erdostw commented on June 1, 2024

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.

agrahl avatar agrahl commented on June 1, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.