コンテンツへスキップ
メニュー
この質問にフラグが付けられました
1 返信
2684 ビュー

how to parse the html code in frontend which is returned from backend?

I wirte very simple code to describe this, 

BACKEND:

# python backend return html code to frontend
def demoFunc():
# module name: simple.demo
# business logic codes
return {'div': "
hello world
"}


Frontend JS:

class SimpleDemo extends Components {
setup(){
...
this.state = useState({ htmlcode: 'default'});
onMounted(async () = > {
await this.orm.call('simple.demo', 'demoFunc', ['']).then((val)= > {
this.state.htmlcode = val;
});
});
}
}


Frontend template:

[templates xml:space="preserve">

    [t t-name="simpledemo.template">

        [t t-esc="state.htmlcode.div" />

    [/t>

[/templates>


(forum editor automatically parse above code so I replaced

problem: 

in template: 

[t t-esc="state.htmlcode.div" />



the raw htmlcode is directly displayed in web page :

[div class='demo'>hello world[/div>

, acutally I want to have it parse this div sentence with class "demo", and display:

"hello world"

 how to parse the html code in front end?


アバター
破棄
最善の回答

Hi,

You should return the html code like this from the backend 

Hello World!


And in the front end

await this.orm.call('simple.demo', 'demoFunc', ['']).then((val)= > {
                        this.state.htmlcode = owl.markup(val);
                });

And in the xml you may need to use t-out instead of t-esc but if it work then it's good to go


Hope it helps

アバター
破棄
関連投稿 返信 ビュー 活動
5
3月 25
2765
1
2月 25
1311
2
7月 25
300
1
5月 25
1061
0
5月 25
633