===================================== 1.7. Document parameters and binding ===================================== Within the content of a document the parser will look for expressions that will be evaluated at binding time into actual values. Every attribute in scryber, and all text can be bound with an expression. .. figure:: ../images/samples_overviewBindingCalc.png :alt: Aggregations and calculations The usual method for specifying these values uses the handlebars syntax - ``{{expression}}``. .. code:: html
{{concat('Hello ',model.userName)}}
For styles and css the ``calc()`` method is extended to used expressions. .. code:: css .banner { background-image: calc(model.logoSource); } This allows the inclusion of dynamic content at runtime either for specific values, for binding onto repeating content, or for evaluating expressions. The values are passed to the document through the ``Params`` property of a document instance. .. code:: csharp doc.Params["model"] = new { userName = "Richard", logoSource = "url(../images/mylogo.png)" }; 1.7.1 Generation methods ------------------- All methods and files in these samples use the standard testing set up as outlined in :doc:`../1_overview/5_samples_reference` 1.7.2 Simple Binding Example ---------------------------- .. code:: html {{title}}
{{title}}.
When processing the document, the value for ``title`` can be provided. .. code:: csharp //Scryber.UnitSamples/OverviewSamples.cs public void SimpleBinding() { var path = GetTemplatePath("Overview", "SimpleBinding.html"); using (var doc = Document.ParseDocument(path)) { doc.Params["title"] = "Hello World"; //Before databinding - value is not set Assert.IsNull(doc.Info.Title); using (var stream = GetOutputStream("Overview", "SimpleBinding.pdf")) { doc.SaveAsPDF(stream); } //After databinding Assert.AreEqual("Hello World", doc.Info.Title); } } At generation time these values will be interpeted and set on the appropriate properties and rendered to the file. As the layout has not executed before the databind, the content will be flowed with the rest of the document. .. figure:: ../images/doc_simple_binding.png :target: ../_images/doc_simple_binding.png :alt: Binding simple content for documents :width: 600px :class: with-shadow `Full size version <../_images/doc_simple_binding.png>`_ .. note:: Scryber is strongly typed. It will try and convert or parse the values on databinding, and most of the style values and properties can be parsed. But the content should be of the correct type. 1.7.3. Complex expressions ---------------------------- As you can imagine the parameters could start to get unmanageable and complex. Thankfully the support for expressions allows both interrogation and calculation. It is possible to use both strongly typed or dynamic objects (or a combination of both) for parameters. And expressions support any depth of property, and also an indexor in brackets. For example the following are all supported. .. code:: csharp model.property model.property[index] model.property[function()].name The classes can be dynamic or strongly typed but the properties are **Case Sensitive** to ensure language compatibility. If properties are not found, then the whole expression will return null. 1.7.4. Binding to complex objects --------------------------------- We can add both, a strongly typed user in the model, and also a dynamic theme object. .. code:: csharp //Scryber.UnitSamples/OverviewSamples.cs public class User { public string Salutation {get;set;} public string FirstName {get;set;} public string LastName {get;set;} } public void ComplexBinding() { var path = GetTemplatePath("Overview", "ComplexBinding.html"); using (var doc = Document.ParseDocument(path)) { var user = new User() { Salutation = "Mr", FirstName = "Richard", LastName = "Smith" }; doc.Params["model"] = new { user = user }; doc.Params["theme"] = new { color = "#FF0000", space = "10pt", align = "center" }; using (var stream = GetOutputStream("Overview", "ComplexBinding.pdf")) { doc.SaveAsPDF(stream); } } } Our template can then access the properties on each of these objects. It can either be used in a function e.g. ``{{concat()}}`` or as a direct value ``{{model.user.FirstName}}`` For styles, the handlebars syntax is supported, but also the ``calc()`` css function. .. code:: html {{concat('Hello ', model.user.FirstName)}}
Hello {{model.user.FirstName}}.
And the output as below. .. figure:: ../images/doc_expression_binding.png :target: ../_images/doc_expression_binding.png :alt: Binding complex content for documents :width: 600px :class: with-shadow `Full size version <../_images/doc_expression_binding.png>`_ 1.7.5. Looping over collections ------------------------------- Along with the interrogation of the object properties scryber supports the enumeration over collections using the ``