This is a migrated thread and some comments may be shown as answers.

How to pass string as a argument in a Template

4 Answers 2842 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Siddhartha
Top achievements
Rank 1
Siddhartha asked on 21 Mar 2014, 02:54 PM
How can I pass a string with single quote in a Kendo grid template - 

In the below code I have a function confirmDelete which expects a string argument, in the example I am passing id (encrypted string) but when I add the single quote around it the HTML rendering fails. If I pass just id with no quotes AngularJs throws a error.

This may not be the right forum but I have not got response from the Kendo Lab, i will appreciate your help.

 k-columns='[
                                                    { "field": "firstName", sortable:true, "title": "First Name"},
                                                    { "field": "lastName", sortable:false,"title": "Last Name"},
                                                    { "field": "middleName", sortable:true,"title": "Middle Name"},
                                                    { "field": "id", sortable:true,"title": "Id"},
                                                    { "template": "<a href=\"Constituent/Detail/#: id#\">Edit</a><a href=\"\" ng-click=\"confirmDelete('\"+id+\"')\">Delete</a>", "title": "Actions" }]'

4 Answers, 1 is accepted

Sort by
0
Mihai
Telerik team
answered on 23 Mar 2014, 09:48 AM
Hi,

There are too many levels of quoting in your code, I suggest you don't even try to get it right in that form as it will be a burden to maintain it.

My recommendation is that you move that columns definition in a scope variable, for example just write: k-columns="gridColumns" in your HTML.  You can also define that column template in HTML:

<script type="text/x-kendo-template" id="my-special-column">
    <a href="Constituent/Detail/#: id #">Edit</a>
    <a href="" ng-click="confirmDelete('#: id #')">Delete</a>
</script>

and in your controller you define the columns:

$scope.gridColumns = [
    { "field": "firstName", sortable:true, "title": "First Name"},
    ...
    {
        template: kendo.template($("#my-special-column").html()),
        title: "Actions"
    }
];

Hope this helps.

Regards,
Mihai
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Sadiq
Top achievements
Rank 1
answered on 15 May 2020, 12:25 PM

Hi,

You can make use of Escape characters ( \ ) inside the template code as show below:

template: "<span class='icon-edit' onclick='submitString(\"\#= anyStringColumnField #\"\);'>Submit</span>"

 

Hope it helps.

0
Pistle
Top achievements
Rank 1
Veteran
answered on 05 Oct 2020, 12:14 PM
 think that what you are asking is how you could do something like the following:
template<string_type str> struct X {}; 
X<"message"> x; 
for some type string_type.
(My answer is going to delve beyond this question though.)
Currently, the short answer is “you cannot”. In C++17 (the current standard language), the best you can do as an approximation is something like:
template<auto &str> struct X { 
static constexpr int N = sizeof(str); 
}; 
extern constexpr char const message[] = "a message"; 
X<message> x; 
However… C++20 is trying to improve that by enabling nontype template parameters of class types.
Prior to the last standardization meeting (earlier this month in Belfast), the solution relied on “opting into the feature” by declaring a “trivial comparison operator”. Something like:
#include <compare> 
#include <algorithm> 
template<int N> struct FixedString { 
constexpr FixedString(char const (&s)[N]) { 
std::copy_n(s, N, this->elems); 


constexpr std::strong_equality 
operator<=>(FixedString const&) const = default; 
char elems[N]; 
}; 
template<int N> FixedString(char const(&)[N])->FixedString<N>; 
which, would permit:
template<FixedString str> struct X { 
}; 
X<"a message"> x; 
There is a bit to unpack here. Remember that a (narrow) string literal is a fixed-length array of char const elements (e.g., “a message” is a 10-element array, including the terminating null). So it can be bound to a reference to array. E.g., I could write:
FixedString<10> msg("a message"); 
C++17 introduced the notion of “class template argument deduction, which allows that last line to be rewritten as:
FixedString msg("a message"); 
because the compiler can deduce N from the constructor signature and the array (i.e., string literal) that is passed to it. That mechanism is going to be extended in C++20 to cover other contexts, including nontype template arguments (see [temp.arg.nontype]/1), and so when I wrote:
X<"a message"> x; 
above, that will (in C++20) trigger the deduction of type FixedString<10> for the actual nontype template parameter of X. Now, prior to this month (November 2019), a nontype template parameter could be of class type provided that the type is a literal type (essentially, a class type that can be constructed and destroyed as a constant-expression) and that it had strong structural equality, which approximately means that it has a “trivial” operator<=> (one where equality is known to be determined by recursively comparing subobject until you reach fundamental types). In the code for FixedString above, I achieved that by providing a = default definition for operator<=> with the “strong equality” return type. See http://wg21.link/P0732r2 for details.
A few of us were quite concerned by that approach to nontype parameters of class type:
equality (i.e., the result of comparing with ==) is really not the mechanism used to compare nontype template arguments, and
that approach would forever close the doors on the possibility of being able to use types like std::vector<int> or std::string for nontype template parameters.
To illustrates my first bullet, consider this code:
enum E { e, f, g }; 
template<E x> struct XE {}; 
constexpr bool operator==(E, E) { return false; } 
XE<e> xe; 
extern XE<e> xe; // Okay. 
This has been valid in every C++ standard so far: The type XE<e> is the same, despite the fact that e == e returns false. There are other examples like that (i.e., it’s not just enumeration types; pointer and pointer-to-member types can do similar things).
In any case, during the comment period for the C++20 committee draft, the US entered an entry requesting this to be revised, and in Belfast a small group of us (David Herring, Jens Maurer, Richard Smith, Jeff Snyder, and myself) worked out a new model for nontype template arguments in general, and class type nontype template arguments in particular. Our plan was to move the base model into C++20, and then work out a generalization for C++23. That plan has been approved, and the C++20 “phase” has been voted in for C++20. Let me talk about both…
For C++20, we’re dropping “equality” as a mechanism to compare template arguments, and instead we introduce the notion of template argument equivalence which you can think of as “X and Y are template-argument-equivalent if the compiler’s constant-evaluator cannot distinguish their values”. I won’t go into the details, but http://wg21.link/P1907r1 should produce the spec in a few weeks. One of the notable extensions is that this allows floating-point template arguments, with +0.0 and -0.0 potentially producing different instances (despite +0.0 == -0.0 in practice). Another one is that structural types include class types that only have public subobjects all the way down: The fact that the (base and member) subobjects are public is an indication that only their representational values matter since anyone can change those values at any time (i.e., the class cannot rely on invariant relations between the members, for example). So, the FixedString example above is still a valid nontype template parameter type, but the operator<=> is now ignored and can be omitted if desired (or be made nontrivial instead). Now, when we write:
X<"a message"> x; 
the compiler will deduce type FixedString<10> as before, and create a compile time object (call it __argobj) of that type for the argument, which will be initialized with FixedString<10>(“a message”) (evaluated at compile time). We’ve recently (i.e., a few days ago) also concluded that we want the compiler to evaluate a copy of __argobj and verify that it is template-argument-equivalent to the original (to catch/diagnose shenanigans occurring in copy constructors). That (immutable) __argobj will then be the object used whenever the corresponding template parameter is referred to in the template itself.
That’s for C++20. For C++23, we’re planning to go one step farther. The plan is to introduce a new special member function that produces a “canonical representation” of class type values. For example, suppose we have a Rational type that is internally represented with non-normalized nominator and denominator values:
class Rational { 
struct Repr { int num, denom; } r; 
public: 
Rational(int n, int d): r{n, d} {} 
... 
}; 
and we want to use it as a template parameter:
template<Rational R> struct Y {}; 
Then we’d want Y<Rational(6, 3)> to be the same type as Y<Rational(4, 2)>. But how do we communicate that to the compiler? The answer is that we introduce a new operator (syntax to be determined) that produces (at compile time) a “normalized value” that is of the kind that is acceptable as a C++20 class type nontype template argument. In our Rational example, Rational::Repr is such a type. Here is how it might look like:
class Rational { 
struct Repr { int num, denom; } r; 
Repr operator<>() { 
// Return a normalized representation: 
return { num/gcd(num, denom), denom/gcd(num, denom) }; 

Rational(Repr r): r(r) {} 
public: 
Rational(int n, int d): r{n, d} {} 
... 
}; 
Now, when we write Y<Rational(6, 3)>, the compiler will (a) evaluate Rational(6, 3), (b) apply operator<>() to it and evaluate that (which here produces the value Repr{ 2, 1 }), (c) evaluate a copy of this value and make sure it’s still Repr{ 2, 1 }, and (d) compute the template parameter value __argobj by converting Repr{ 2, 1 } to Rational. Whether two template arguments are equivalent will now be decided based on the intermediate representation object. So, since Y<Rational(4, 2)> also produces Repr{ 2, 1 } it will be the same type as Y<Rational(6, 3)>. In addition, we’re also planning to introduce a “magic” non-fixed-length array type that can be used to build intermediate representations, which will close the loop to enable std::string or std::vector<int> template argument types. All that is a bit complicated, admittedly, but the upshot of it is that hopefully in C++23 we’ll be able to write:
#include <string> 
template<std::string str> struct Z {}; 
Z<"finally!"> z; 
0
Alex Hajigeorgieva
Telerik team
answered on 07 Oct 2020, 12:36 PM

Hello, Pistle,

Thank you for sharing your in depth knowledge with the Kendo UI community.

Just a quick note to make an alternative suggestion -  pass the element which triggered the event and obtain the dataItem() or any of its properties as demonstrated in this forum thread:

https://www.telerik.com/forums/pass-a-parameter-to-an-event-handler-inside-a-template#Nz8G8UBLoUadHM2CFux26w

I hope this helps.

Kind Regards,
Alex Hajigeorgieva
Progress Telerik

Five days of Blazor, Angular, React, and Xamarin experts live-coding on twitch.tv/CodeItLive, special prizes, and more, for FREE?! Register now for DevReach 2.0(20).

Tags
General Discussions
Asked by
Siddhartha
Top achievements
Rank 1
Answers by
Mihai
Telerik team
Sadiq
Top achievements
Rank 1
Pistle
Top achievements
Rank 1
Veteran
Alex Hajigeorgieva
Telerik team
Share this question
or