Creating Dynamic Templates

Learn how to create dynamic templates with placecode


Creating dynamic templates with Placecode is a powerful way to streamline your project development by customizing code blocks, files, and folders based on selected features. In this guide, we'll cover how to create dynamic templates using comment markers, manage files and folders, handle dependent features, and publish your dynamic templates for the community to use.

Note: If you're unfamiliar with comment markers and their usage, it's recommended to refer to the Guide: Using Comment Markers for Dynamic Templates section first to get a clear understanding before proceeding with this guide.

For File Contents

As discussed in the Guide: Using Comment Markers for Dynamic Templates section, comment markers play a vital role in associating code blocks with specific features. You can use comment markers in your file contents to indicate code blocks that belong to particular features.

  1. Identify the code block you want to make dynamic based on certain features.
  2. Wrap the code block with placecode comment markers.

For example, to make a code block relevant to feature1, use the following comment markers:

// pc:begin: feature1
// Your code block for feature1 goes here
// pc:end: feature1

Please refer to the Guide: Using Comment Markers for Dynamic Templates to learn more about using multiple features in comment markers.

During the dynamic template running process using the placecode run command, if feature1 is not selected, the code blocks wrapped with the comment markers will be commented out temporarily (use placecode re command to revert back). These code blocks will also be excluded from the template generation using the placecode gen command.

Please note that the placecode gen command is only used to generate existing dynamic templates from cloud repositories and should not be used in the template creation process.

For Files and Folders

You can also use the same comment marker style to assign files and folders to specific features. To do this, you need to add a special text file named zpc.txt (with the .txt extension) in the same directory level as the files or folders you want to specify for certain features.

For example, let's assume the following scenario:

In this case, you have a feature called feature1, and you want the foo folder and bar.js file to be included in the template only when feature1 is selected.

To achieve this, you have to create a zpc.txt file in the same directory level as foo and bar.js is located.

project
├── foo
│   ├── foo-file-1.js
│   ├── foo-file-2.js
│   └── foo-file-3.js
├── bar.js
└── zpc.txt

The zpc.txt file content would look like this:

// pc:begin: feature1
for
bar.js
// pc:end: feature1

As shown in the example, the zpc.txt file uses the same comment marker style as used in code blocks. Instead of code content inside the comment wrapper, we define the files and folder names on new lines.

The zpc.txt file should be added to every directory where you want to have dynamic files or folders. For example, if you want to make only the file foo-file-1.js dynamic and include it only when feature1 is selected, place the zpc.txt file inside the foo folder and specify the file name inside the zpc.txt file.

// pc:begin: feature1
foo-file-1.js
// pc:end: feature1

Please refer to the "Guide: Using Comment Markers for Dynamic Templates" to learn more about using multiple features in comment markers.

You can have multiple zpc.txt files in your project. Each zpc.txt file defines the files and folders for the corresponding directory level. This allows you to fine-tune the dynamic template generation process for different parts of your project separately.

To simplify the process of adding zpc.txt files, you can use the placecode addzpc command, which automatically adds empty zpc.txt files to every directory in your project. Any empty zpc.txt files will be automatically deleted during the placecode run and placecode re commands, ensuring a clean and organized template creation process.

During the dynamic template running process using the placecode run command, if a feature is not selected, the files and folders specified in the corresponding zpc.txt file will be temporarily removed (you can revert this using placecode re). These files and folders will also be excluded in the template generation using the placecode gen command.

Please note that the placecode gen command is only used to generate existing dynamic templates from cloud repositories and should not be used in the template creation process.

Dependent Features

Dependent features play a crucial role in creating dynamic templates. A feature might depend on another feature or category (a category contains multiple similar features) to exist or work properly.

To specify dependent features, define them in the placecode.json file under attributes depend_features and depend_categories. During the dynamic template generation process using the placecode gen command, if a feature depends on other features. It will thoroughly check if every depend feature is selected beforehand selecting a specific feature.

Publishing

Once you have created a dynamic template, you may want to share it with others. Publishing a dynamic template is a seamless and easy process. Please refer to the Publishing: Dynamic Templates section in the documentation for more details on how to share your dynamic templates with the world.