Code Example


File name: tests/test_3_1.cpp.
All other examples you can found in catalog tests.

Source code:

// C++ Includes
#include <iostream>
// Local Includes
#include <ctpp/ctpp.hpp>

int main(void)
{
    using namespace template_parser_ns;
    using namespace std;

    // We create structure of the data
    param_data * pData = new param_data(param_data::HASH);
    // Insert global parameter
    pData -> hash_insert_new_var("global_param", "Global Parameter");

    // Insert cycle
    param_data * pArray = pData -> hash_insert_new_array("EMPLOYEE_INFO");

    // Inside a cycle - hash with parameters
    param_data * pHash = pArray -> array_insert_new_hash();
    // Parameter "NAME"
    pHash -> hash_insert_new_var("NAME", "Ivanov Ivan");
    // Parameter "JOB"
    pHash -> hash_insert_new_var("JOB", "The architect");

    pHash = pArray -> array_insert_new_hash();
    pHash -> hash_insert_new_var("NAME", "Petrov Petr");
    pHash -> hash_insert_new_var("JOB", "The builder");
    // We substitute global parameter
    pHash -> hash_insert_new_var("global_param", "Replaced Parameter");

    try
    {
        // Templae engine
        template_text oTemplate;

        // Includes Loader
        loader_base * pLoaderBase = new loader_base();

        // Allowed list of catalogs to include
        v_include_dir vIncludeDir;
        vIncludeDir.push_back("./tmpl");
        oTemplate.set_include_dir(vIncludeDir);
        // Template File
        pLoaderBase -> load_file("./tmpl/3_1.tmpl");
        // Parse the template
        oTemplate.parse(pLoaderBase -> get_data());
        // We impose parameters on a pattern
        oTemplate.param(pData);
        string sOutput;
        // Result
        sOutput.assign(oTemplate.output());
        cout << sOutput << endl;
    }
    catch(exception &e) { cout << e.what() << endl; exit(1); }
return 0;
}



Template:

<TMPL_include header.tmpl>
<TMPL_var global_param>
<TMPL_loop __CONTEXT_VARS__ __GLOBAL_VARS__ EMPLOYEE_INFO>
    <TMPL_var NAME> - <TMPL_var JOB>
    <TMPL_var global_param>
    <TMPL_unless IN_SET(NAME, "Ivanov Ivan")>
        TEST_ANY_PARAM
    </TMPL_unless>
</TMPL_loop>
<TMPL_include footer.tmpl>


Parameters:

global_param => "Global Parameter"
EMPLOYEE_INFO
[
    { NAME => "Ivanov Ivan", JOB => "The architect" },
    {
NAME => "Petrov Petr", JOB => "The builder", global_param => "Replaced Parameter" }
]

Opuput:
<!-- Copyright (c) 2004 Andrei V. Shetuhin -->
<html>
<head>
    <title>Sample</title>
</head>
<body>
Global Parameter

    Ivanov Ivan - The architect
    Global Parameter


    Petrov Petr - The builder
    Replaced Parameter

        TEST_ANY_PARAM


</body>
</html>
<!-- Footer End -->

Compilation:


gcc -c test_3_1.cpp -I /usr/local/include/ctpp

For dynamic linking:
gcc test_3_1.o -L /usr/local/lib -o test_3_1 -lctpp

For static linking:
gcc -static test_3_1.o -L /usr/local/lib -o test_3_1 -lctpp -lstdc++


Copyright © 2003 - 2005 CTPP Dev. Team | http://reki.ru/products/ctpp