Comment Markers

Using Comment Markers for Dynamic Templates


Comment markers are the key element in creating dynamic templates with placecode. They serve as a consistent commenting style used to define code blocks, files and folders corresponding to specific features mentioned in the placecode.json file.

Comment Marker Structure

A comment marker is a simple and easily understandable way to identify and separate features in your project or codebase. It follows a specific structure:

// pc:begin: feature1
// Code block for feature1 goes here
// pc:end: feature1

All placecode comment markers must start with // pc:. This is the only comment style that placecode recognizes. Make sure you do not use // pc: as a prefix for other comments in your codebase to avoid conflicts.

Please note that every start comment marker should have a corresponding ending marker. The // pc:begin: marker for a specific feature must be followed by a // pc:end: marker with the same feature label. This ensures proper pairing of code blocks with their respective features during dynamic template creation.

Support for the # prefix comment marking style will be released soon to accommodate languages like Python, offering even more flexibility for dynamic template creation.

Single Feature Comment Marker

A comment marker wraps around a code block to indicate that this block belongs to a specific feature. Below is an example of a comment marker for feature1:

// pc:begin: feature1
console.log("This code block is for feature1")
// pc:end: feature1

Multiple Feature Comment Marker

A comment marker can include multiple feature names separated by commas. In the example below, the code block will be included in the dynamic template if either feature2 or feature3 is selected:

// pc:begin: feature2, feature3
console.log("This code block is for feature2 and feature3")
// pc:end: feature2, feature3

There is no limit to the number of features you can include in a single comment marker.

Multiple Feature Comment Marker sometimes may not work as expected. Please avoid using parent nested comment markers with multiple features.

Nested Comment Markers

Nested comment markers are a powerful feature of Placecode CLI that allows you to use comment markers within a parent comment marker. This feature enables even more granular control over code blocks based on specific combinations of features.

Here's an example to illustrate how nested comment markers work:

// pc:begin: feature1
console.log("this is a code line for feature1")
 
// pc:begin: feature2
console.log("this is a code line for feature2")
 
// pc:begin: feature3, feature4
console.log("this is a code line for feature3 and feature4")
// pc:end: feature3, feature4
 
console.log("this is a code line for feature2")
// pc:end: feature2
 
console.log("this is a code line for feature1")
// pc:end: feature1

Consistency in Comment Markers

To ensure a smooth dynamic template generation process, it's crucial to maintain consistency with comment markers.

Feature labels in the begin marker should exactly match those in the corresponding end marker. All feature labels mentioned in the begin marker must also be included in the end marker.

❌ Invalid Comment Markers

The following examples show invalid comment markers that should be avoided:

// pc:begin: feature2, feature-3
console.log("This is an invalid comment marker")
// pc:end: feature2, feature3
 
// pc:begin: feature2
console.log("This is an invalid comment marker")
// pc:end: feature2, feature3
 
// pc:begin: feature2
console.log("This is an invalid comment marker")
// pc:end:

Usage for Files and Folders

In the examples above, we've demonstrated how to use comment markers within the content of a file. Refer to the documentation on how to use comment markers for files and folders, where you can keep specific files or folders based on the features selected.

For further information on the behavior of comment markers and other commands, please refer to the common reference for placecode run, placecode re, and placecode gen.

By consistently following the comment marker style, you can effectively manage and customize your dynamic templates using placecode.