简体   繁体   中英

Can I fill a websharper template's hole without knowing its name at compile time

I am new using WebSharper and I am stuck in the current situation with WebSharper 4:

I have the following html template (some-template.html) :

<div class="container">
    <div ws-replace="Content">
    </div>
</div>

It defines a content hole with the name Content . Usually, one could fill it using the following in code (F#):

type SomeTemplate = WebSharper.UI.Templating.Template<"some-template.html">
let doc = SomeTemplate().Content(someElements)

In my scenario, I do not know the name of the template and the hole at compile time. Suppose I have a function:

let buildDom(tempalte : string, holeName : string, content : Doc list) : Doc =
    let template = WebSharper.UI.Template<tempalte> // (1)
    // template.FillHole(holeName, content) (2)

I do not know how to best deal with (1) -- creating the template, and (2) - locating and filling the hole. Reflection comes to mind, but I would like to know if there is a more elegant and performant approach.

A more general question -- is there a good way to have dynamic composition of html-templated sitelets? This is what I am trying to achieve, but if it is alredy done there could be no need to reinvent the wheel. I'd appreciate if you point me to such resources if available as well.

You can pass a dynamic template by passing a string to the constructor:

type SomeTemplate = WebSharper.UI.Templating.Template<"some-template.html">

let doc = SomeTemplate("<div>...</div>").SomeHole("some content").Doc()

but the holes are still typed statically based on the type provider. There is currently no API to implement dynamic holes.

This could be a nice and useful addition though; you should post a suggestion on github .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM