How can I render string as html in using jTemplate - asp.net-mvc-3

I am using asp.net C# MVC jTemplate binding Announcements collection to a template in my view.
One of the announcement has html string as in its description.
Now the problem is how to render that string as html
my view is like
<textarea id="dashAnnoucement_Template"
data-template-name="dashAnnoucement_Template" class="hidden">
{#foreach $T as Announcement}
<li id='{$T.Announcement$iteration}'>
{#if !$().compare($T.Announcement.Category,
#Convert.ToInt32(UI.Entities.AnnouncementCategory.RichText))}
<div class="card large flipped">
<div class="pic">
{#if $T.Announcement.LinkOff}
{#if $T.Announcement.Image.IsVideo}
<a href="{$T.Announcement.LinkOff.Target}" class="play">
<span>{$T.Announcement.Image.AltText}</span>
<img alt="{$T.Announcement.Image.AltText}"
src="{$T.Announcement.Image.Source}" class="thumb"/>
</a>
{#else}
<a href="{$T.Announcement.LinkOff.Target}">
<img alt="" src="{$T.Announcement.Image.Source}" class="thumb"/>
</a>
{#/if}
{#else}
<img alt="" src="{$T.Announcement.Image.Source}" class="thumb"/>
{#/if}
</div>
<div class="content"><h3>
{#if $T.Announcement.LinkOff}
<a href="{$T.Announcement.LinkOff.Target}"
target="{#if $T.Announcement.LinkOff.IsExternal}_blank{#else}_self{#/if}">
{$T.Announcement.Title}</a>
{#else}
{$T.Announcement.Title}
{#/if}
</h3>
<p>{$T.Announcement.Summary}</p>
</div>
</div>
{#else}
{$T.Announcement.Description}
#*Need to render this as html*#
{#/if}
</li>
{#/for}
</textarea>
jquery code is like
$(Container).empty()
.setTemplateElement(Template)
.processTemplate(announcementsCollection);
My Json Data i.e announcementsCollection is like
[ { "Category": 1, "Title": "White Space Announcement Title", "Description": "", "Summary": "Join us Fri,April 24 in Atlanta,GA to mingle with Solavei members & hear top earner success stories", "Image": { "Source": "http://slvdev.blob.core.windows.net/media/1463/whitespace_1.jpg", "AltText": "", "Target": "", "IsVideo": false }, "Date": "\/Date(-62135596800000)\/", "LinkOff": { "Target": "http://solavei.com/", "Text": "Learn More >", "IsExternal": true } }, { "Category": 1, "Title": "Got Questions?", "Description": "", "Summary": "Access Member Support for information on your phone, your Solavei Membership, and your Solavei Mobile Service.", "Image": { "Source": "http://slvdev.blob.core.windows.net/media/7323/equipment-support-224x168.jpg", "AltText": "", "Target": "", "IsVideo": false }, "Date": "\/Date(-62135596800000)\/", "LinkOff": { "Target": "http://support.solavei.com", "Text": "Learn More >", "IsExternal": true } }, { "Category": 4, "Title": "html", "Description": "<div class=\"imageBox shadowBox\" style=\"float: right; margin: -20px -20px 0px 20px;\"> \t <a target=\"_blank\" href=\"https://support-uat.solavei.com\">\t\t \t\t <img class=\"fullImage\" alt=\"\" src=\"http://slvdev.blob.core.windows.net/media/35985/neon.jpg\" style=\"height:170px\">\t </a> </div>\n<div class=\"contents\"> <h2> <a target=\"_blank\" href=\"https://support-uat.solavei.com\"> Rich text </a> </h2> <p>Access Member Support for information on your phone, your Solavei Membership, and your Solavei Mobile Service.</p> </div>", "Summary": "<div><img src='https://slvdev.blob.core.windows.net/media/35985/neon.jpg' wid/>\n<p>Test Message</p></div>", "Date": "\/Date(-62135596800000)\/" } ]

I know this is an old post but,
First you have to transform your JSon data into Html code and then
use Html.Raw("") to show the code as Html.
#Html.Raw("Your html in here")

Related

Vue.js 3 Uncaught (in promise) TypeError: Cannot convert undefined or null to object

So, I'm trying to fetch my laravel api and already run php artisan serve. After that the error came out and I'm looking to find solution here but I have no idea what's going on.
The error shown in the console:
Uncaught (in promise) TypeError: Cannot convert undefined or null to object
The API:
{
"message": "List trash and category order by time",
"data": [
{
"id": 1,
"trash_id": 1,
"category_id": 2,
"created_at": "2022-01-01T12:41:43.000000Z",
"updated_at": "2022-01-01T12:41:43.000000Z",
"garbage": {
"id": 1,
"name": "Buku",
"weight": 1,
"created_at": "2022-01-01T12:41:19.000000Z",
"updated_at": "2022-01-01T12:41:19.000000Z"
},
"categories": {
"id": 2,
"name": "Kertas",
"price": 1800
}
}]
}
Index.vue script:
<script>
import axios from "axios";
import { onMounted, ref } from "vue";
export default {
setup() {
// reactive state
let all_trash = ref([]);
onMounted(() => {
// get data from api endpoint
axios
.get("http://127.0.0.1:8000/api/trash")
.then((result) => {
all_trash.value = result.data;
})
.catch((err) => {
console.log(err.response);
});
});
return {
all_trash,
};
},
};
</script>
Index.vue HTML:
<div v-for="(trash, index) in all_trash.data" :key="index">
<div class="card rounded shadow mb-3">
<div class="card-body">
<div class="mx-3">
<h5 class="mb-4">{{ trash.garbage.name }}</h5>
<p>
{{ trash.categories.name }}
<span class="float-end">
<button class="btn" style="color: red">Hapus</button>
</span>
</p>
</div>
</div>
</div>
</div>
Just add a condition that not to render the loop until data is not set. The data from API end point is not available on the initial dom rendered and there is not data in all_trash.data hence the error. Just add a v-if on top of the div and hopefully it should work.
<div v-if=" all_trash.data">
<div v-for="(trash, index) in all_trash.data" :key="index">
<div class="card rounded shadow mb-3">
<div class="card-body">
<div class="mx-3">
<h5 class="mb-4">{{ trash.garbage.name }}</h5>
<p>
{{ trash.categories.name }}
<span class="float-end">
<button class="btn" style="color: red">Hapus</button>
</span>
</p>
</div>
</div>
</div>
</div>

Laravel bind data on vue tamplate

I am trying to bind user_name to Vue from this Laravel Object:
{ "order": [ { "id": 87, "user_id": "2", "user_name": "Mohamed Hassan", "table": 20, "total": 224, "status": 1, "delivered": 0, "paied": 0, "created_at": "2019-09-14 15:04:40", "updated_at": "2019-09-14 15:04:40" } ] }
and this object get from:
{{order.data}}
I also tried with this:
{{order.data['order']['user_name']}}
but it returns null
and this is my Vue template
<li v-for="order in orders">
<div class="col-md-3 col-sm-3 col-xs-3"><div class="notify-img">{{order.data}}</div></div>
<div class="col-md-9 col-sm-9 col-xs-9 pd-l0">
Date:
{{order.created_at}}
<i class="fa fa-dot-circle-o"></i>
<p>Lorem ipsum sit dolor amet consilium.</p>
<p class="time">1 Saat önce</p>
<hr>
</div>
</li>
If I see it properly, then the order is actually an array holding a single object.
You could try:
{{ order.data['order'][0]['user_name'] }}

SmartAdmin DataTables - how to import ajax data from component.ts

I am using the SmartAdmin Angular 5 template to render a DataTable. The SmartAdmin datatable example, which I have modified to be LoginHistory, gets its ajax data from a JSON file stored in the web app, as I show below. This works fine. But, how can I modify it to get its data from a string defined in component.ts?
loginhistory.component.html
<div id="content">
<div class="row">
<sa-big-breadcrumbs [items]="['Table', 'Data Tables']" icon="table"
class="col-xs-12 col-sm-7 col-md-7 col-lg-4"></sa-big-breadcrumbs>
</div>
<sa-widgets-grid>
<div class="row">
<article class="col-sm-12">
<div sa-widget [editbutton]="false" color="blueDark">
<header>
<span class="widget-icon"> <i class="fa fa-table"></i> </span>
<h2>Export to PDF / Excel</h2>
</header>
<div>
<div class="widget-body no-padding">
<sa-datatable
[options]="{
ajax: '../../assets/api/tables/datatables.standard.json', // <<<<<<< HOW TO USE loginHistoryData FROM loginhistory.component.ts????
columns: [
{data: 'id'},
{data: 'name'},
{data: 'phone'},
{data: 'company'},
{data: 'zip'},
{data: 'city'},
{data: 'date'}
],
buttons: [
'copy', 'excel', 'pdf', 'print'
]
}"
tableClass="table table-striped table-bordered table-hover"
>
<thead>
<tr>
<th data-hide="mobile-p">ID</th>
<th data-class="expand">Name</th>
<th>Phone</th>
<th data-hide="mobile-p">Company</th>
<th data-hide="mobile-p,tablet-p">Zip</th>
<th data-hide="mobile-p,tablet-p">City</th>
<th data-hide="mobile-p,tablet-p">Date</th>
</tr>
</thead>
</sa-datatable>
</div>
</div>
</div>
</article>
</div>
</sa-widgets-grid>
</div>
loginhistory.component.ts
import { Component, OnInit } from '#angular/core';
import { FadeInTop } from "../../shared/animations/fade-in-top.decorator";
#FadeInTop()
#Component({
selector: 'sa-datatables-case',
templateUrl: './loginhistory.component.html',
})
export class LoginHistoryComponent implements OnInit {
public loginHistoryData: any;
constructor() {}
ngOnInit() {
this.loginHistoryData =
+ '{'
+ ' "data": ['
+ ' {'
+ ' "id": "1",'
+ ' "name": "Jennifer",'
+ ' "phone": "1-342-463-8341",'
+ ' "company": "Et Rutrum Non Associates",'
+ ' "zip": "35728",'
+ ' "city": "Fogo",'
+ ' "date": "03/04/14"'
+ ' },'
+ ' {'
+ ' "id": "2",'
+ ' "name": "Clark",'
+ ' "phone": "1-516-859-1120",'
+ ' "company": "Nam Ac Inc.",'
+ ' "zip": "7162",'
+ ' "city": "Machelen",'
+ ' "date": "03/23/13"'
+ ' },'
+ ' ]'
+ '}';
}
}
A little trial & error solved the problem. Just needed to change the ajax label to data, and include the name of the component.ts parameter.
I also cleaned up the loginHistoryData parameter by using the typescript nomenclature instead of a string.
loginhistory.component.html
<div id="content">
<div class="row">
<sa-big-breadcrumbs [items]="['Table', 'Data Tables']" icon="table"
class="col-xs-12 col-sm-7 col-md-7 col-lg-4"></sa-big-breadcrumbs>
</div>
<sa-widgets-grid>
<div class="row">
<article class="col-sm-12">
<div sa-widget [editbutton]="false" color="blueDark">
<header>
<span class="widget-icon"> <i class="fa fa-table"></i> </span>
<h2>Export to PDF / Excel</h2>
</header>
<div>
<div class="widget-body no-padding">
<sa-datatable
[options]="{
data: loginHistoryData, //<<<< change ajax to data and add the name of the component.ts parameter
columns: [
{data: 'id'},
{data: 'name'},
{data: 'phone'},
{data: 'company'},
{data: 'zip'},
{data: 'city'},
{data: 'date'}
],
buttons: [
'copy', 'excel', 'pdf', 'print'
]
}"
tableClass="table table-striped table-bordered table-hover"
>
<thead>
<tr>
<th data-hide="mobile-p">ID</th>
<th data-class="expand">Name</th>
<th>Phone</th>
<th data-hide="mobile-p">Company</th>
<th data-hide="mobile-p,tablet-p">Zip</th>
<th data-hide="mobile-p,tablet-p">City</th>
<th data-hide="mobile-p,tablet-p">Date</th>
</tr>
</thead>
</sa-datatable>
</div>
</div>
</div>
</article>
</div>
</sa-widgets-grid>
</div>
loginhistory.component.ts
import { Component, OnInit } from '#angular/core';
import { FadeInTop } from "../../shared/animations/fade-in-top.decorator";
#FadeInTop()
#Component({
selector: 'sa-datatables-case',
templateUrl: './loginhistory.component.html',
})
export class LoginHistoryComponent implements OnInit {
public loginHistoryData: any;
constructor() {}
ngOnInit() {
this.loginHistoryData = [
{
"id": "1",
"name": "Jennifer",
"phone": "1-342-463-8341",
"company": "Et Rutrum Non Associates",
"zip": "35728",
"city": "Fogo",
"date": "03/04/14"
},
{
"id": "2",
"name": "Clark",
"phone": "1-516-859-1120",
"company": "Nam Ac Inc.",
"zip": "7162",
"city": "Machelen",
"date": "03/23/13"
}
];
}
}

How to make a dynamic input field as a numeric textbox in Kendo UI?

Here is my situation.
I am making a dynamic form with a help of this article.
Here you can see it (article demo) use kendo template .
<script id="fieldsTemplate" type="text/x-kendo-template">
<li>
<label data-bind="attr: { for: name}, text: label"></label>
<input data-bind="attr: { type: type, name: name, class: css}" # if (get("required")) {# required #} # />
</li>
</script>
After form generated this form is just use HTML5 make the form. It does't have kendo attribute. for a example if I bound data-role attribute and value is numerictextbox It doesn't give me a numeric text box(Think its' type is number). It doesn't have those properties.( if I type a number it doesn't show the default decimal point. It only shows that number.)
But in this example says if we add data-role attribute and value as numerictextbox it will be a numeric text box.
But in documentation or in this , it seems I have to call kendoNumericTextBox method to make a numeric text box.
Even I try to add this code to template but it doesn't work(Please assume that I add this correctly with this ).
$("#mytextboxid").kendoNumericTextBox();​
So what option do I left ??
Thank you very much
You have to set data-role attribute into input element to convert the element into kendo control/element. Please try with the below code snippet.
<body>
<div id="example">
<!-- container UL to host input fields -->
<ul data-template="fieldsTemplate" data-bind="source: fields"></ul>
</div>
<!-- Kendo Template binds properties from model to input fields -->
<script id="fieldsTemplate" type="text/x-kendo-template">
<li>
<label data-bind="attr: { for: name}, text: label"></label>
<input name=#= name # class=#= css # # if (get("required")) {# required #} #
# if (type == 'number') {# data-role="numerictextbox" #}else{# type=#=type# #}# />
</li>
</script>
<script type="text/javascript">
// retrieve the model from an API call
var model = {
"fields": [{
"css": "cssClass1",
"label": "Field 1",
"name": "Field1",
"required": false,
"type": "text"
},
{
"css": "cssClass2",
"label": "Field 2",
"name": "Field2",
"required": true,
"type": "number"
},
{
"css": "cssClass2",
"label": "Checkbox Field",
"name": "CheckField",
"required": false,
"type": "checkbox"
},
{
"css": "cssClass2",
"label": "Email Address",
"name": "Email",
"required": true,
"type": "email"
},
{
"css": "cssClass2",
"label": "Password",
"name": "Password",
"required": true,
"type": "password"
}
]
};
// convert the JSON to observable object
var viewModel = kendo.observable(model);
// bind the model to the container
kendo.bind($("#example"), viewModel);
</script>
</body>
JSFiddle
Let me know if any concern.

How would I render a list with Mandrill template out of array passed in MERGE VARS

I have to make substitution with merge_vars.
{
"key":"some key",
"template_name":"order-confirmation",
"template_content":[
{
"name":"ORDERNUMBER",
"content":"12312312321"
},
{
"name":"PRICE",
"content":"35.10"
},
{
"name":"NAME",
"content":"Some name"
},
{
"name":"PICKUPDATE",
"content":"2013-05-10"
},
{
"name":"ORDERITEMS",
"content":[
{
"NUMPRODUCTS":"26",
"PRODUCTNAME":"Milk",
"PRODUCTPRICE":"1.35 EUR"
}
]
},
{
"name":"SERVICENUMBER",
"content":"12345"
},
{
"name":"PICKUPPOINT",
"content":"AAA 350"
},
"message":{
"from_email":"support#support.com",
"to":[
{
"email":"asdasd#asda.com"
}
],
"subject":"Subject text",
"attachments":[
]
},
"async":false
}
}
how should I make html placeholders? I did it like this but it doesn't work.
I'm only interested in ORDERITEMS.
<tr>
<td>*|ORDERITEMS:NUMPRODUCTS|*</td>
<td>*|ORDERITEMS:PRODUCTNAME|*</td>
<td>*|ORDERITEMS:PRODUCTPRICE|*</td>
</tr>
As far as I know, using the normal templating engine in Mandrill, the only supported type for the content attribute is string, so there is no way to provide a structure.
Recently, (Jan 13 2015) Mandrill announced support for handlebars templates, and with that, the posibility to include arrays in the content.
http://blog.mandrill.com/handlebars-for-templates-and-dynamic-content.html
With this new template language it is possible to not only access sub-propertys as you need, but even loop inside arrays, and even nested do loops. (see this comment on the blog post)
This is an example extracted from the blog post for doing a loop:
The merge variable or template content:
{
"name": "products",
"content": [
{
"img": "http://kbcdn.mandrill.com/nesting-penguin.png",
"qty": 2,
"sku": "PENG001",
"name": "Penguin",
"description": "Solid wood, hand-painted penguin nesting doll with 5 different sizes included. Limited Edition.",
"price": "12.99",
"ordPrice": "25.98"
},
{
"img": "http://kbcdn.mandrill.com/nesting-bear.png",
"qty": 3,
"sku": "BBEAR001",
"name": "Brown bear",
"description": "Solid wood, hand-painted brown bear nesting doll. Coordinates with our entire Bear collection. Includes 6 nested sizes.",
"price": "12.99",
"ordPrice": "38.97"
}
]
}
And this is how to consume it in the template:
{{#each products}}
<tr class="item">
<td valign="top" class="textContent">
<img src="{{img}}" width="50" height="75" class="itemImage" />
<h4 class="itemName">{{name}}</h4>
<span class="contentSecondary">Qty: {{qty}} x ${{price}}/each</span><br />
<span class="contentSecondary sku"><em>{{sku}}</em></span><br />
<span class="contentSecondary itemDescription">{{description}}</span>
</td>
<td valign="top" class="textContent alignRight priceWidth">
${{ordPrice}}
</td>
</tr>
{{/each}}
In your case, you could do:
{{#each ORDERITEMS}}
<tr>
<td>{{NUMPRODUCTS}}*</td>
<td>{{PRODUCTNAME}}</td>
<td>{{PRODUCTPRICE}}</td>
</tr>
{{/each}}
In case someone has these same problem, and the doc solution does not work, check the field merge_language in your message json. It has to be defined to handlebars.

Resources