Wtx ~ Wt Extension Library
WtxLib
Binding Variables to Strings

There are a couple handy methods for quickly formatting strings using the WString and WTemplate objects. When formatting a simple small string with just a few parameters, then this function works real well:

64 auto posggres_connect =
65 Wt::WString("host={1} port={2} dbname={3} user={4} password={5}")
66 .arg( "localhost" )
67 .arg( 5433 )
68 .arg( "tradewindb" )
69 .arg( "twdb" )
70 .arg( "xFoElc442!!" )
71 .toUTF8()
72 ;ยท

That formats the string exactly how you want it, and the source is super readable and therefore easy to maintain. The function converts argument types of strings and integers and doubles alike. This is good for simple 1-line formatted strings.

If the string is longer and more complex then it is useful to employ named placeholders. That will require the use of the built-in WTemplate object. Given the following formatted string, put placeholders everywhere there should be a bound string variable:

<message id="cbc.pull_request">
<data_area>
<header_data>
<user_id>${user_id}</user_id>
<user_pwd>${user_password}</user_pwd>
<cus_id>${customer_id}</cus_id>
<xml_format>1</xml_format>
<action>${bureau_action}</action>
</header_data>
<applicant_data>
<applicant type="primary">
<person_name>
<first_name>${first_name}</first_name>
<middle_name>${middle_name}</middle_name>
<last_name>${last_name}</last_name>
</person_name>
<address_data>
<address type="current">
<line_one>${street_address}</line_one>
<city>${city}</city>
<state_or_province>${state}</state_or_province>
<postal_code>${zipcode}</postal_code>
</address>
</address_data>
<social>${social_security_number}</social>
</applicant>
</applicant_data>
</data_area>
</message>

Using the following procedure, load the formatted string in to the WTemplate object, bind the string values, and then extract the formatted HTML using the 'htmlText' function call.

Wt::WTemplate templt( Wt::WString::tr("cbc.pull_request") );
templt.bindString( "user_id", "Ben" );
templt.bindString( "user_password", "ABC123!!" );
templt.bindString( "customer_id", "421123" );
templt.bindString( "bureau_action", "softpull" );
templt.bindString( "first_name", "John" );
templt.bindString( "middle_name", "" );
templt.bindString( "last_name", "Smith" );
templt.bindString( "street_address", "123 W. Ave M" );
templt.bindString( "city", "Bigtown" );
templt.bindString( "state", "Bigstate" );
templt.bindString( "zipcode", "50505" );
templt.bindString( "social_security_number", "123-45-6789" );
std::stringstream ss;
templt.htmlText( ss );
COUT_( ss.str() );

Will produce the following result:

<div id="otojh9a">
<data_area>
<header_data>
<user_id>Jim</user_id>
<user_pwd>Password1!!</user_pwd>
<cus_id>CL14124</cus_id>
<xml_format>1</xml_format>
<action>softpull</action>
</header_data>
<applicant_data>
<applicant type="primary">
<person_name>
<first_name>John</first_name>
<middle_name></middle_name>
<last_name>Smith</last_name>
</person_name>
<address_data>
<address type="current">
<line_one>123 West St</line_one>
<city>Bigtown</city>
<state_or_province>Montanna</state_or_province>
<postal_code>50424</postal_code>
</address>
</address_data>
<social>123-45-6789</social>
</applicant>
</applicant_data>
</data_area>
</div>

Note that this is simple string binding. Note also that the resulting 'string' also includes html tags

at the top and bottom of the output string, so if those are not wanted (they probably arent) then those are going to have to get removed. But, if you do need to format an html-compliant string, this is a fast way to do it.