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

raise exception if variable is not defined in context for a template #307

Open
EmadHelmi opened this issue Aug 10, 2022 · 5 comments
Open

Comments

@EmadHelmi
Copy link

say this is my template file:

<html>
  <head>
    <title>Our admins and users</title>
  </head>
  {# This is a short example to give you a quick overview of pongo2's syntax. #}
  {% macro user_details(user, is_admin=false) %}
  <div class="user_item">
    <!-- Let's indicate a user's good karma -->
    <h2 {% if (user.karma>= 40) || (user.karma > calc_avg_karma(userlist)+5) %} class="karma-good"{% endif %}>

      <!-- This will call user.String() automatically if available: -->
      {{ user }}
    </h2>

    <!-- Will print a human-readable time duration like "3 weeks ago" -->
    <p>This user registered {{ user.register_date }}.</p>

    <!-- Let's allow the users to write down their biography using markdown;
            we will only show the first 15 words as a preview -->
    <p>The user's biography:</p>
    <p>
      {{ user.biography }}
      <a href="/user/{{ user.id }}/">read more</a>
    </p>

    {% if is_admin %}
    <p>This user is an admin!</p>
    {% endif %}
  </div>
  {% endmacro %}

  <body>
    <!-- Make use of the macro defined above to avoid repetitive HTML code
      since we want to use the same code for admins AND members -->

    <h1>Our admins</h1>
    {% for admin in adminlist %} {{ user_details(admin, true) }} {% endfor %}

    <h1>Our members</h1>
    {% for user in userlist %} {{ user_details(user) }} {% endfor %}
  </body>
</html>

and in this simple code I want to render the template

tpl, err := pongo2.FromFile("sample.tpl")
if err != nil {
		log.Fatal(err)
	}
ctx := pongo2.Context{}

In a way I want to get an error if the defined context does not match the variables in the template or get used variable names in the template. How can I do this?

@flosch
Copy link
Owner

flosch commented Aug 10, 2022

This is not yet supported.

@EmadHelmi
Copy link
Author

This is not yet supported.

So you mean that there is no way even to get the identifier names in a template?
I saw some parsing tools but all of them are locally used. I should write a local function but I need to access the template variables. Isn't there any way?

@flosch
Copy link
Owner

flosch commented Aug 10, 2022

So you mean that there is no way even to get the identifier names in a template?

Yes, that's correct. There is no official or supported way to do so. I don't know about custom solutions though.

@knavels
Copy link

knavels commented Aug 10, 2022

There are 2 issues in here:

  1. Method for getting tokens is not exported nor template's token
  2. It seems the tokenozer mixes all identifiers and keywords in one place as identifier for example if I have a variable called user in my template and also I have used if these 2 shouldnt be recognized as a similar token type first is identifier and the other is keyword

@knavels
Copy link

knavels commented Aug 10, 2022

If these issues are resolved then yes we can implement what @EmadHelmi needs manually

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

No branches or pull requests

3 participants