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

Make [[[end]]] optional to allow inline templating #12

Open
tw39124-1 opened this issue Mar 30, 2020 · 1 comment
Open

Make [[[end]]] optional to allow inline templating #12

tw39124-1 opened this issue Mar 30, 2020 · 1 comment

Comments

@tw39124-1
Copy link

tw39124-1 commented Mar 30, 2020

Firstly, thanks for Cog, it is such a great way to generate files against a template using Python.

However, the major downside of the current implementation (for me at least) is that it is line-based. There doesn't appear to be a way to do inline templating. For example, let's say I want to generate a Doxygen C source file header:

[[[cog import cog ]]] [[[end]]]
/**
 * @file [[[cog cog.out(filename) ]]] [[[end]]]
 * @copyright Copyright (c) [[[ cog.out(current_year + " " + company_name) ]]] [[[end]]]
 * @author [[[cog cog.out(file_author) ]]] [[[end]]]
 */

However, the Cog source code mandates that the [[[end]]] tag is present, and is on a different line to the Python code block, which prevents this rather useful ability. If the -d option is used to delete the source code and the [[[end]]] tag was optional, I could just do:

/**
 * @file [[[cog cog.out(filename) ]]]
 * @copyright Copyright (c) [[[ cog.out(current_year + " " + company_name) ]]]
 * @author [[[cog cog.out(file_author) ]]]
 */

which would get transformed rather neatly into:

/**
 * @file my_first_file.h
 * @copyright Copyright (c) 2020 AcmeCorp
 * @author Joe Bloggs
 */

This is kinda similar to how PHP works inline with HTML, and would be a really useful feature IMO.

@kiteloopdesign
Copy link

Yes, I guess its not that nice, but you can still do like so
(dont think it compiles but you get the idea ...)

/**
[[[cog import cog 
 filename = FILENAME
company_name = COMPANY_NAME 
]]]
 * print (f'@file{FILENAME})
 * print (f'@file{COMPANY_NAME})* 
 */ [[[end]]]

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

No branches or pull requests

2 participants