Is it a bad practice to use a template engine to restructure a JSON object? Of course i can do it manually but i want to avoid the null check route if possible.
Update: Can those who downvoted at least explain why?
For something like XPath for JSON, use JSONPath:
http://goessner.net/articles/JsonPath/
https://github.com/s3u/JSONPath
JSONPath produces an array of items matching the selection. You can combine that with JavaScript array forEach method to do most of what is typically done in XSLT templates.
Alternatively, you can use jsonpath-object-transform which provides a little bit more in the way of templates:
https://github.com/dvdln/jsonpath-object-transform#readme
Related
I am trying to use MatPaginator but with a normal 'table' tag in html instead of 'mat-table'. It didnt seem to work and when I looked up for some references, I found all the examples related to MatPaginator makes use of the MatTableDataSource. Is it compulsory that both should be clubbed and used for the pagination to work?
MatTableDataSource is aimed to be used for filtering, sorting and pagination of a client-side data array. In cases where filtering, sorting and pagination are done server-side, the use of this class is not appropriate.
A good example is given at https://blog.angular-university.io/angular-material-data-table/. It doesn't use MatTableDataSource but still uses mat-table, this should however be easily adaptable to a normal HTML table.
How do I fetch data using a temporary view with ektorp. I would like to pass the map javascript function to couchdb as a string and get the results. (This is definitely possible with CouchDB4J)
Fortunately CouchDB have well-documented API and you can do this using HttpURLConnection. That's not the easiest way, but that will work in any case
I'm using Require.js and the text plugin. I'm trying to create a template that has something like this:
<%= somefunction(displayvalue) %>
I need the somefunction(...) to be available to multiple templates. How is the best way to wrap the function to use in multiple places? I was thinking of making it a module, but I don't know how to pass it to a template.
After you compile a template, you find yourself with a function to call, and you pass in some arguments. So just pass your function as argument:
var tpl = _.template( tplString );
tpl({ someFunction: function( val ) { /* do something */ } });
Although, I think you're probably better if you only pass value inside your template data. Template are actually way easier to debug and maintain if they're almost logic less. So, instead, I'd go like this:
tpl({ someValue: someFunction( aValue ) });
In other javascript templating engine (like Handlebars), you can actually register helpers functions who'll be mostly available globally to execute action on your template data. If you really need to use the same function inside multiple templates, I'd think about switching template engine. Underscore provide a micro-templating engine, and as so, is somehow limited around helpers functions - although being able to contain way too much logic...
In my opinion, underscore template works well for small project and should be use carefully. Keep them as simple as possible: if/else, printing data, and that should be it. If you need more, go for a more complete template engine.
But even there, all logic you do inside a template is hard to debug.
So! Keep it simple.
Is there an easy way to count the nodes in a HTML file? I also need to count nodes of a certain type such as div etc.
I'd like to do this if possible without having to use an external library like HTMLAgilityPack if possible. Also, the HTML I'm dealing with is not guarenteed to be well formed and valid.
Is there a way to do this from C#?
Thanks.
first of all. are your sure a client-side solution using javascript isn't sufficent to your needs?
because the easiest way to count nodes within an HTML document is using jQuery on the client-side browser.
<script src="http://code.jquery.com/jquery-1.7.min.js"></script>
<script>
$('html').children() // will give you all child elements of the html element
$('body').children() // same for body element
$('body').children('div') // will give you just the direct children elements of 'div' type
$('body').find('div') // will give you all the nested elements of 'div' type
</script>
if you are unfamilier with jQuery then take a look at www.jquery.com
if u still need a C# solution for server-side parsing of the document then then i would recommend to use HTMLAgilityPack (even thou you wish not to). writing your own parser seems to me like a waste of time as you need to consider malformed html/xml and such which can be a pain.
try and use this s-overflow article: What is the best way to parse html in C#?
hope it will satisfy your needs
If you have XHTML you can load it in a XDocument and use XML manipulation API or LINQ to XML to count the particular modes.
If you don't you can try using Regular Expressions. But this one works in small number of interesting tags since you have to define manually an expression for each tag.
With LinqToXml API, you can easily parse and loop through all the nodes of an HTML document. You can find helpful articles related to LinqToXml but all in context of parsing XML documents.
Following is a similar thread from StackOverflow : C# Is there a LINQ to HTML, or some other good .Net HTML manipulation API?
What's the best way to escape data from Models or Controllers to easily and safely display them in views. It seems kind of overkill to use html::specialchars($varname) for every data variable. It can also cause problems if a programmer forgets to "escape" data.
I've also encountered problems escaping ORM objects within loops.
I wrote the Twig module gimpe has suggested and by default it automatically escapes all data. You might also want to look into Kostache. It's a class based view system that does automatic escaping.
Regarding your comment:
Is there a way to do this directly from the Model
You don't want to escape the data here because HTML escaped data doesn't make sense in all output formats, eg: JSON and XML.
Do the escaping at the view level.
One way to achieve that is using a templating engine like Twig for the views. (see KO3 module http://github.com/ThePixelDeveloper/kohana-twig)
Then you simply need to load the Escaper extension:
Twig_Extension_Escaper: Adds automatic output-escaping and the possibility to escape/unescape blocks of code.
Ref.: http://www.twig-project.org/book/03-Twig-for-Developers