[Lazarus] getlazarus.org search request

Anthony Walter sysrpl at gmail.com
Mon Aug 12 10:33:11 CEST 2019


Michael,

Here is an example of how easy it is.

    public class CodeSearch : TemplateHandler
    {
        private static readonly string searchSql =
"search.sql".LoadResourceText();

        protected override void Run(Templates templates, StringBuilder
output)
        {
            SearchItems = DataCommand
                .Prepare(searchSql)
                .Add("@phrase", Read("phrase"))
                .Compose();
            templates[Read("format") + "SearchItem"].FormatObject(this,
output);
        }

        public IEnumerable<object> SearchItems { get; set; }
    }

So given the url,
https://docs.getlazarus.org/?method=codesearch&format=xml&phrase=TStringList in
steps:

1) The library automatically finds the CodeSearch class without any code to
write
2) It runs the template handler automatically
3) In run we execute some SQL to search for a phrase and compose the
results as a collection of anonymous objects
4) The we select the template we want and format the CodeSearch object and
its available properties, in this case our search result "SearchItems".

The FormatObject method is a template engine that uses reflection to lookup
properties with mustache braces. It has the ability to query subproperties
and use all available format specifiers. Collection properties (IEnumerable
<T>) are expanded by a detail template that can read through reflection the
properties of the anonymous objects in the collection by name and template
them as well.

Here are some template examples. Note you can access sub properties using
object.suprop.susubprop syntax as well as custom formatting in the template
(i.e. Price:C2 or Page.Modified:yyyyy-mm-dd).

<title>{Title}</title> or <title>{Page.Title}</title> // Welcome to our
website
<div>Price is {Amount:C2}</div> // Price is $15.75
<div>Page modified on {Page.Modified:yyyyy-mm-dd}</div> // Page modified on
2019-07-10
<div>{SeachItems}</div> // Expands our template
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20190812/ff9436ee/attachment.html>


More information about the lazarus mailing list