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

Expression support #117

Open
svanimpe opened this issue May 27, 2017 · 3 comments
Open

Expression support #117

svanimpe opened this issue May 27, 2017 · 3 comments
Labels

Comments

@svanimpe
Copy link
Member

svanimpe commented May 27, 2017

Are there any plans to support simple expressions like {{ variable + 1 }} ?

When rendering a template, I set a page variable and wanted to use expressions like this to generate links to the previous and next pages. I created previous and next filters to work around this limitation.

@kylef kylef added the question label May 27, 2017
@kylef
Copy link
Collaborator

kylef commented May 27, 2017

I haven't planned to add expressions, expression support would encourage placing programming logic your templates. Stencils design goals are meant to express presentation and not programming logic.

In your case, wouldn't that type of logic need to know if there is a next and previous page instead of blindly assuming you can + or - 1 to find a closest sibling page? If so, I think it would make more sense to create some kind of "paginator" which handles all the logic so that a template can just deal with presentation of pagination instead of the underlying logic behind pagination.

For example:

protocol Paginator {
  var hasPrevious: Bool { get }
  var hasNext: Bool { get }

  var nextURL: String? { get }
  var previousURL: String? { get }

  var currentPageIndex: Int { get }
  var totalPages: Int { get }
}
{% if paginator.hasPrevious %}
  <a href="{{ paginator.previousURL }}">Previous Posts</a>
{% endif %}

Current page {{ paginator.currentPageIndex }} out of {{ paginator.totalPages }}.

{% if paginator.hasNext %}
    <a href="{{ paginator. nextURL }}">More Posts</a>
{% endif %}

@svanimpe
Copy link
Member Author

@kylef I was using currentPage and hasNextPage as simple pagination info. That was the minimum information I needed. The web front-end already knows the URL of the endpoint, so pagination was as simple as currentPage - 1 if currentPage > 0 and currentPage + 1 if hasNextPage. But you're right, that wasn't very clean design :)

I was mostly wondering if you've come across other use cases that might need expression support (or similar extra features)?

@shad0w-jo4n
Copy link

@svanimpe For example, I needed even elements of the array to be displayed definitely, and odd elements in a different way, but the order of the output elements was the same as in the array. And using a class or structure to store the parity/odd of an element I don't think is rational.

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

3 participants