cancel
Showing results for 
Search instead for 
Did you mean: 

Enable XML Templating

trmt
Explorer

Hi forum!

Cn someone please explain briefly how to implement XML Templating in an SAP UI5 project? I feel that is basically impossible to get this solved based on only the documentation.

I want to conditionally render one element or the other in a Fragment something like:

<core:FragmentDefinition
	xmlns="sap.m"
	xmlns:core="sap.ui.core"
	xmlns:template="http://schemas.sap.com/sapui5/extension/sap.ui.core.template/1">
    <template:if test="{myData>/condition}">
        <template:then>
            <Button
                text="{i18n>connectBtn}" 
                icon="sap-icon://citizen-connect" 
                press="onConnectPressed"
                ariaHasPopup="Dialog"
            />
        </template:then>
        <template:else>
            <Text text="Connection not possible" />
        </template:else>
    </template:if>
</core:FragmentDefinition>

The problem that I have while inspecting the view:

sap-ui-core.js:60 GET https://ui5.sap.com/1.96.14/resources/http://schemas/sap/com/sapui5/extension/sap/ui/core/template/1... net::ERR_ABORTED 404
failed to load JavaScript resource: http://schemas/sap/com/sapui5/extension/sap/ui/core/template/1/if.js -  sap.ui.ModuleSystem

I know I'm missing something in the Controller even though I'm not sure this error has anything to do with it.

I get my data in an API call like:

_getData: function () {
    const oView = this.getView()

    let  reqSettings = {
        "url": `/usersURL/`,
        "method": "GET",
        "headers": {
            "Content-Type": "application/json"
        }
    }

    jQuery.ajax(reqSettings).done(function (response) {
        oView.setModel(
            new JSONModel(response),
            "myData"
        )
    })
}

What am I missing in the controller to enable the XML preprocessor?

In the documentation there is this:

var oTemplateView = sap.ui.view({
    preprocessors: {
      xml: {
        bindingContexts: {
          meta: oMetaModel.getMetaContext(sPath)
        },
        models: {
          meta: oMetaModel
        }
      }
    },
    type: sap.ui.core.mvc.ViewType.XML,
    viewName: "sap.ui.core.sample.ViewTemplate.tiny.Template"
  })
 
oTemplateView.setModel(oModel)
oTemplateView.bindElement(sPath)

But I'm not sure where and how to use this. Everything I've tried is not really working.

Thank you!

View Entire Topic
junwu
Active Contributor
0 Kudos

you have to use that code to process the template, before the template can be used in your app