You have been redirected from an outdated version of the article. Below is the content available on this topic. To view the old article click here.

Dynamic Files

This feature allows users to use template expressions or scripting in text files as it works in the page rendering engine.

There’s a boolean switch labeled Is Template. If activated, template expressions and scripts are evaluated at runtime.

fig

In the integrated file editor, the switch is triggered by the checkbox labeled Replace template expressions:. Please make sure to have saved the content before activating the switch, and pay attentation to not overwrite the template expression by the evaluation result.

In the following example, we created a simple text file containing My path is: ${this.path}. Depending on the location of the file in Structr’s virtual filesystem, it displays the file’s path dynamically.

fig

If you move the file into a subdirectory, the text changes accordingly.

fig

The expression/script evaluation does not depend from the way you access the file, so you also get the dynamic content when accessing the file over FTP or SSH/SCP.

Please note that you should use this feature with care. It is recommended to use it only with read-only file access as saving the evaluation result would overwrite the template expression or script.

Update: In versions after 2017-07-31 the content of the edit-mode window will always show the source code for admin users. Regular users will always get the evaluated version and will not be able to see the source code when the flag is set. Admin users can see the evaluated file by opening it in a new tab.
This means that there is no need for special caution when saving the contents of the file as the editor window will always display the source code.

Search results for "Dynamic Files"

Edit File

The other two parameters, Replace <a data-id="c535223d2c1e409ebdcbbfcf1479c15f" class="mention">template</a> expressions and Show preview, are used to configure so-called dynamic files. Additionally to storing static content, as in the case of the text file above, Structr also allows you to open a scripting context within a file and thus inject dynamically generated content. Such files, containing dynamically generated content, are referred to as dynamic files.

Built-in Properties

The two properties Content Type and Don't Cache are particularly relevant for dynamic files. The content type controls which type of syntax highlighting is applied to the file’s content in the editing dialog, and the don’t cache property makes sure that the scripts in the dynamic file are evaluated every time it is accessed.