Skip to content

Instantly share code, notes, and snippets.

@mertyildiran
Last active March 19, 2021 17:01
Show Gist options
  • Save mertyildiran/32cd275e5084cbd6757c1942025ef2fa to your computer and use it in GitHub Desktop.
Save mertyildiran/32cd275e5084cbd6757c1942025ef2fa to your computer and use it in GitHub Desktop.
How render_part() works?

How render_part() works?

render_part() method in recognizers.py module is not doing just plain template rendering.

It does, in order:

  • Automatic Regex Conversion
  • Template Rendering (with regEx is the only available/injected method in the context)
  • Variable Detection

{{varname}} variable detection cannot be handled by template rendering since varname is not known before hand and it cannot be added to the context. Therefore it should be extracted with a regex {{(.*?)}} after the template rendering happens, or vice versa.

Here are some examples that categorizes the rendering types:

Path

/{{var1longname}}/prefix-{{var2}}-middle-{{var3}}/{{regEx('(.*)_(.*)', 'var4', 'var5')}}
 ^                ^                               ^
 ┆Variable Det.   ┆Automatic Regex Conversion     ┆Template Rendering

Query String

?param1={{var1longname}}&param2=prefix-{{var2}}-middle-{{var3}}&param3={{regEx('(.*)_(.*)', 'var4', 'var5')}}
        ^                       ^                                      ^
        ┆Variable Det.          ┆Automatic Regex Conversion            ┆Template Rendering

In conclusion, as it can be understand from the examples above; applying template rendering to HTTP components as a whole is not possible because it's just not plain template rendering. There are additional logic. So it needs to be parsed into key-value pairs first then the value needs to be passed into render_part() method.

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