I'd like to use something like this in StringTemplate - to call a function defined in the application that does the rendering, is that possible?
<div> $renderArticle(article,width,padding)$ </div>
I could use a custom type/class just for this field but that seems a bit hacked togheter and not a real solution to the problem, and it wont allow me to pass in arguments as far as i know.
I want this becuase i want to allow users to customize templates, for example: i know the how much width i have available for the article but i want the user to be able to specify padding so that i can generate optimal image thumbnails inside that template.
Or, if is it possible to use simple arithmetics, that would be another solution to the problem, something like this perhaps:
... width="$width-50$ ...
I have been looking all over for this and not found any information...
I'd rather use StringTemplate because the application already uses it, but i could switch to something else if it's more suitable, maybe i should take a look at NVelocity?
As I82Much says in his comment, StringTemplate doesn't support calculations.
One solution is to enrich your model with a new property whose value is pre-calculated as 'width-50'.
If you're looking for an alternative, and you're using .net 4.0, you can host the razor view engine and use it as your templating solution.
Related
Please bear with me for the length of this question, I tried to be as descriptive as possible. I am new to WPF programming. I am basically trying to build the RadDataFilter control:
RadDataFilter is used to filter collection by building query expression. The control is generally used by bringing all data from the database and then filtering on them.
However, I am trying to use it to build SQL query expression and then I query the database to retrieve the data. Our company has license for Telerik products so I had to try and use the control. Unfortunately, the control is not flexible to the extent that I would want to customize the Left Dropdown (RadComboBox).
As we have several items which users can filter on, it is not feasible to provide them with a single dropdpwn. I would like to have a custom control instead of the dropdown.
I have the following workaround to this problem:
Extract the Template for that control and override it in my XAML as described here. However, inspite of doing this, I am unable to change the control. I assume it is due to my inadequate knowledge in WPF.
Try to change the Source code for Telerik (We have the license for the code too). However with this approach, we need to keep making changes with every upgrade, which is high maintenance for the team.
Try to build a custom control.
I am looking for the 3rd approach. If there is an easy way (using the first 2 approaches) or any other solution to achieve this, please let me know and I shall try that.
To implement the 3rd approach, I am thinking to use a TreeView with each node using a ControlTemplate (containing my Custom Control). I will override the ToString() method of the Custom Control that gives me the one query condition. Once done, I would iterate through the children of the root node to build the final query expression.
However as I notice, we need 2 different ControlTemplates (one which allows creating a filter expression and the other with the operator, similar to RadDataFilter). I do not know how to set multiple templates like this.
This may sound simple to some of the developers out there, but I am really new to C# and WPF programming and would need time and experience to be comfortable with it.
Please let me know if you know any other solution that I can try to solve this problem. If you feel it is better to create a custom control, please guide me on whats the best way to implement this. Also comment on my approach to create the custom control.
Thanks!
I resolved it. Took the first approach:
"Extract the Template for that control and override it in my XAML as described here.."
I apologize in advance for the generic nature of my question, but I was unable to find any helpful advice from people trying to do the same thing as me on the web. Let me describe my scenario:
I am providing end users/designers of a website the ability to customize their views by storing the views (using Razor) in the database. I have all of this working, but my question is the following; From a security standpoint, how can I ensure and enforce that unwanted code doesn't get executed in the user-defined view? There are two basic approaches that I think will work conceptually, but am not sure which one is more possible or feasible.
Option 1: Create a validation method in the administration tool that allows the user to input the view code. This would need to either take a whitelist or blacklist approach to what is allowable or not.
Option 2: Prevent unwanted code from being able to execute when rendering of the view occurs.
As a quick example of something that would need to be blocked, we wouldn't want to allow access to read or write files, access any data access functions, or even access configuration settings, etc. in the web.config. There will likely be a decently-sized list of things that probably shouldn't be allowable, but I'll need to sit down and try to think of as many security-related concerns as possible.
My question then is, which method would be the best bet? Also, can any direction be provided on how to go about either? I thought I might be able to make trust-level based change which would be Option 2, but couldn't find any way to make that work in a per-view based manor (the administration code is allowed to execute whatever it wants). I'm thinking Option 1 will end up being the best bet and I'll have to check for the input of certain framework functions that shouldn't be allowed. Does anyone have any experience doing anything like what I'm trying to do? ANY feedback is much appreciated!
This would be extremely difficult.
You could run the the template through the Razor preprocessor, then use Roslyn (still in early beta) to parse the generated file and look through all method calls (or constructors) and return an error if it calls something you don't like.
I strongly recommend that you use a whitelist for that, since the .Net framework is big enough that you are bound to overlook something in a blacklist.
However, I would instead recommend that you not use Razor at all and instead use a templating engine that does not allow real C# code.
I'm just curious if there is any software, that could help me with mock management and creation - I prefer Java and .net but I'll be happy to see it for any other languages.
My imagination could be something like this:
A simple wizard that can look into class, generate simple Excel style editor where you can add new mock with different values set, default values etc...
Easy access to this mock via say dot notation and index for example:
MockRepository.getMock => platform.domain.person[person_name="John"]
So, is there something at least little similar around?
Yes, this is called an Auto-Mocking Container.
I'm not aware of any centralized, stable resource that defines the term, but just do a web search for the term and lots of articles are going to come up.
Update: I got tired of the lack of a proper pattern description, so I wrote one.
For reference, it looks like this. It's similar to a progressbar except instead of the usual 0%-XX% filled in, this can have any arbitrary range filled in. I tried searching but did not know how to describe it.
I would say you likely won't find a generic control out there to do that.
However, the concept is simple, you could roll your own fairly easily using GDI+
I've taken over a C# (2.0) code base that has the ability to print information. The code to do this is insanely tedious. Elements are drawn onto each page, with magic constants representing positioning. I imagine the programmer sitting with a ruler, designing each page by measuring and typing in the positions. And yes, one could certainly come up with some nice abstractions to make this approach rational. But I am looking at a different method.
The idea is that I'll replace the current code that prints with code that generates static HTML pages, saves them to a file, and then launches the web browser on that file. The most obvious benefit is that I don't have to deal with formatting-- I can let the web browser do that for me with tags and CSS.
So what I am looking for is a very lightweight set of classes that I can use to help generate HTML. I don't need anything as heavyweight as HTMLTextWriter. What I'm looking for is something to avoid fragments like this:
String.Format("<tr><td>{0}</td><td>{1}</td></tr>", foo, bar);
And instead take have this kind of feel:
...
table().
tr().
td(foo).
td(bar)
Or something like that. I've seen lightweight classes like that for other languages but can't find the equivalent (or better) for C#. I can certainly write it myself, but I'm a firm believer in not reinventing wheels.
Know anything like this? Know anything better than this?
Just as an idea: why do you want to assemble the HTML in your applications code? Sounds a bit tedious to me. You could aggregate the data needed for the report and pass this on to one of the template engines (that are "normally" used for web apps) existing for C#. Then you save the result of the template engine to a html file.
The main benefits I see with this approach:
separates view from business logic
html templates can be edited by non-C# developers, html/css knowledge is enough
no need to recompile the application if the html changes
I havent used it yet, but I heard that the Spark View Engine is OK: http://sparkviewengine.com/ (not sure if it is for C# 2.0 though)
Some time ago I experimented (in PHP) with Gagawa ( http://code.google.com/p/gagawa/ ), where you can do stuff like:
$div = new Div();
$div->setId("mydiv")->setCSSClass("myclass");
$link = new A();
$link->setHref("http://www.example.com")->setTarget("_blank");
$div->appendChild( $link );
But soon dropped such an approach in favor of an template engine.
Another approach is converting the data to XML and applying an XSL stylesheet. In order to change the HTML formating you just need to replace the stylesheet.