Text templates: "embedded c++"

M

Markus Dehmann

I'd like to process text or document templates that use "embedded
C++". Here is a constructed example (the texts I have in mind are much
longer and contain relatively few code blocks):

--------TEXT TEMPLATE--------
<CI>#include <iostream>
#include "LetterDataContainer.hpp"
using namespace std;
LetterDataContainer d;</CI>
Dear <C>if(d.isMale()) cout << "Sir";
else cout << "Madam";</C>
as you may have noticed, you still owe me some<C>if(d.getAmount() >
1000){cout << " considerable";}</C> amount of money. Please pay
<C>cout << d.getUrgency();</C>.
--------TEXT TEMPLATE--------

So, it's a text template containing several C++ code blocks that refer
to some data in an object. Every block of code is within <C></C> tags.
The <CI></CI> tags contain header code that is needed as a header for
every code block file.

Now, a program could extract each code block and compile it into an .o
file. Each .o file would contain the header code and one method, for
example sth like
void block_001(){if(d.getAmount() > 1000){cout << " considerable";}}

Then, it would alternately print a piece of text and execute a code
block, print the next piece of text, execute the next code block and
so on.

Is there a better way to process document templates? Or is there a
library that does sth like this? Of course, it's easier to do such a
processing and eval thing in Perl (see
http://perl.plover.com/Template), but the data I have is gathered by a
C++ object.

Thanks!
Markus
 
J

John Harrison

Markus Dehmann said:
I'd like to process text or document templates that use "embedded
C++". Here is a constructed example (the texts I have in mind are much
longer and contain relatively few code blocks):

--------TEXT TEMPLATE--------
<CI>#include <iostream>
#include "LetterDataContainer.hpp"
using namespace std;
LetterDataContainer d;</CI>
Dear <C>if(d.isMale()) cout << "Sir";
else cout << "Madam";</C>
as you may have noticed, you still owe me some<C>if(d.getAmount() >
1000){cout << " considerable";}</C> amount of money. Please pay
<C>cout << d.getUrgency();</C>.
--------TEXT TEMPLATE--------

So, it's a text template containing several C++ code blocks that refer
to some data in an object. Every block of code is within <C></C> tags.
The <CI></CI> tags contain header code that is needed as a header for
every code block file.

Now, a program could extract each code block and compile it into an .o
file. Each .o file would contain the header code and one method, for
example sth like
void block_001(){if(d.getAmount() > 1000){cout << " considerable";}}

Then, it would alternately print a piece of text and execute a code
block, print the next piece of text, execute the next code block and
so on.

Is there a better way to process document templates? Or is there a
library that does sth like this? Of course, it's easier to do such a
processing and eval thing in Perl (see
http://perl.plover.com/Template), but the data I have is gathered by a
C++ object.

Thanks!
Markus

Despite the fact that your code blocks are C++, I think it would still be
best to use perl or some other scripting language to do the processing. Its
just the sort of thing scripting languages are good at.

john
 
M

Markus Dehmann

Despite the fact that your code blocks are C++, I think it would still be
best to use perl or some other scripting language to do the processing. Its
just the sort of thing scripting languages are good at.

I think you are aright. I thought about it again, and I think with
perl I could transform the template into one C++ file. The separate
compilation for each code block is just prohibitive. Instead, a perl
script could turn the text blocks into cout commands and in this way
transform the whole text template file into one source file that can
be compiled and exevuted.

Thanks
Markus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top