I have the following code in a view:
#if (SiteSession.SubPageHelper.DisplayType == DisplayType.List)
{
Html.Partial("_SubLandingPage_List");
}
else
{
Html.Partial("_SubLandingPage_Grid");
}
and within the partials I just have a foreach loop like this:
#foreach (Product product in SiteSession.SubPageHelper.PagedProducts)
{
some html code here
}
Where PagedProducts is got from doing a .Take() on a cached list of products
Now the above code doesn't display my paged products but if I change the partial to include the at symbol remove the semi-colon:
#Html.Partial("_SubLandingPage_Grid")
It will display the products properly. Can anyone tell me what the difference between the two version are as it took me ages to figure out why the products weren't displaying
It is actually razor syntax to tell that we are starting to write c# code, if your don't put # it will be considered as plain text so you need to put # sign before writing c# code in the view and in html helper method you don't need to put semicolon in front of then it is razor syntax to write helpers this way.
For Example:
#Html.LabelFor(x=>m.SomeProperty) // here # is telling that we are writing c# statement
When you write:
#if (SiteSession.SubPageHelper.DisplayType == DisplayType.List)
{
Html.Partial("_SubLandingPage_List"); // this is wrong syntax
}
else
{
Html.Partial("_SubLandingPage_Grid");
}
the right way is to tell that this is a razor html helper and c# statement:
#if (SiteSession.SubPageHelper.DisplayType == DisplayType.List)
{
#Html.Partial("_SubLandingPage_List")
}
else
{
#Html.Partial("_SubLandingPage_Grid")
}
you can see more on razor syntax HERE
Few more links which will help you understanding razor:
http://www.asp.net/web-pages/tutorials/basics/2-introduction-to-asp-net-web-programming-using-the-razor-syntax
http://weblogs.asp.net/scottgu/asp-net-mvc-3-razor-s-and-lt-text-gt-syntax
http://weblogs.asp.net/scottgu/introducing-razor
UPDATE:
An alternative can be to use RenderPartial which will work in the if statement without putting # sign:
#if (SiteSession.SubPageHelper.DisplayType == DisplayType.List)
{
Html.RenderPartial("_SubLandingPage_List");
}
else
{
Html.RenderPartial("_SubLandingPage_Grid");
}
For understanding Difference between Html.Partial and Html.RenderPartial, visist these links:
Html.Partial vs Html.RenderPartial & Html.Action vs Html.RenderAction
http://dotnethelpers.wordpress.com/2013/06/18/difference-between-html-renderpartial-vs-html-partial-and-html-renderaction-vs-html-action-in-mvc/
http://www.em64t.net/2010/12/razor-html-renderpartial-vs-html-partial-html-renderaction-vs-html-action-what-one-should-use/
Well simply the "#" symbol instructs Razor that it have to render something. The # always expect something to print from the supplied statement.
If you don't want to use this syntax you can still render the content with minor changes in your code in loop .. look at this.
#if (SiteSession.SubPageHelper.DisplayType == DisplayType.List)
{
Html.RenderPartial("_SubLandingPage_List"); // this is wrong syntax
}
else
{
Html.RenderPartial("_SubLandingPage_Grid");
}
Html.RenderPartial will directly write it to stream and doesn't requires #.
Even if you try to write something like following
#Html.RenderPartial("_SubLandingPage_Grid")
This will give you an error as # expect something to return and Html.RenderPartial returns void
Related
I don't even have a clue why this is happening. The if condition in my job is not working properly. I triple checked the $flag variable and it is true. Still the code always goes to the else condition.
$flag = file_exists($csvFile);
if($flag){
//A big chunk of code
} else {
return 'Whatever you do I will always go here XD';
}
I hard coded true inside if, it works properly. Then another if inside that chunk of code also always goes to else. Again double checked Express. Express object exists and object is returned properly. And is_object returns true.
$flag = file_exists($csvFile);
if(true){
//Some code here
$entity = Express::getObjectByHandle('user');
if(is_object($entity)){
// Another chunk of code
} else {
return 'You cant escape from me that easily... XD';
}
//some code here
} else {
return "Now I'm a good boy";
}
Tried a bunch of things. The code works without any problems in single page controllers. What ever I do it's just not working. I'm running PHP 5.6.10 on MAMP. Conceret5 8.1.0
The only time I've seen something like this is with a buggy older version of PHP.
You could try moving some code around like so:
if($flag = file_exists($csvFile)) {
// code to handle file
} else {
// no file
}
A followup post would help...
Well. Looks like I found the fix. It looks like a simple matter but don't know for sure. I was running the job from dashboard, I didn't knew I could run the same job from the url as well (Automate job option URL). It worked fine when I ran it from URL and Magically it started working inside the Dashboard as well. I will look into it in detail when I get the time.
how would it be possible to bypass functions that are not existing in DM
such that the main code would still run? Try/catch does not seem to work, e..g
image doSomething(number a,number b)
{
try
{
whateverfunction(a,b)
}
catch
{
continue
}
}
number a,b
doSomething(a,b)
Also conditioning wont work, e.g..
image doSomething(number a,number b)
{
if(doesfunctionexist("whateverfunction"))
{
whateverfunction(a,b)
}
}
number a,b
doSomething(a,b)
thanks in advance!
As "unknown" commands are caught by the script-interpreter, there is no easy way to do this. However, you can construct a workaround by using ExecuteScriptCommand().
There is an example tutorial to be found in this e-book, but in short, you want to do something like the following:
String scriptCallStr = "beep();\n"
scriptCallStr = "MyUnsaveFunctionCall();\n"
number exitVal
Try { exitVal = ExecuteScriptString(scriptCallStr ); }
Catch { exitVal = -1; break; }
if ( -1 == exitVal )
{
OKDialog("Sorry, couldn't do:\n" + scriptCallStr )
}
else
{
OKDialog( "All worked. Exit value: " + exitVal )
}
This works nicely and easy for simple commands and if your task is only to "verify" that a script could run.
It becomes clumsy, when you need to pass around parameters. But even then there are ways to do so. (The 'outer' script could create an object and pass the object-ID per string. Similarly, the 'inner' script can do the same and return the script-object ID as exit-value.)
Note: You can of course also put doesfunctionexist inside the test-script, if you do only want to have a "safe test", but don't actually want to execute the command.
Depending on what you need there might also be another workaround solution: Wrapper-functions in a library. This can be useful if you want to run the same script on different PCs with some of which having the functionality - most likely some microscope - while others don't.
You can make your main-script use wrapper methods and then you install different versions of the wrapper method script scripts as libraries.
void My_SpecialFunction( )
{
SpecialFunction() // use this line on PCs which have the SpecialFunction()
DoNothing() // use alternative line on PCs which don't have the SpecialFunction()
}
My_SpecialFunction( )
I have used this in the past where the same functionality (-stage movement-) required different commands on different machines.
I'm creating own inspections based on Structural Replace.
For example I want to make inspection of transforming code like:
if (!$Map$.containsKey($key$)){
$Map$.put($key$, $value$);
}
and
if ($Map$.get($key$) == null){
$Map$.put($key$, $value$);
}
into
$Map$.putIfAbsent($key$, $value$);
But I don't want it to react on code like:
if (!$Map$.containsKey($key$)){
$Map$.put($key$, $value$);
}
else {
// any logic
}
I tried to use
if (!$Map$.containsKey($key$)){
$Map$.put($key$, $value$);
}
$else$
with option text "else" but it didn't work.
Is it possible? Also I have to make two different inspections with same replace result. Can we use multiple searh pattern?
UPDATE:
I tried replace next pattern
$Iterable$.forEach($value$ -> {
if ($condition$) {
$statement$;
}
});
into
$Iterable$.stream()
.filter($value$ -> $condition$)
.forEach($value$ -> $statement$);
But after replace I'm getting:
$Iterable$.stream()
.filter($value$ -> $condition$)
.forEach($value$ -> $statement$;);
Is it possible to remove ";" from replace result?
Use a search template like this:
if (!$Map$.containsKey($key$)){
$Map$.put($key$, $value$);
} else {
$statement$;
}
Edit variables and set the minimum and maximum count of statement to 0.
Using multiple search patterns for a single Structural Search Inspection is not possible at this time.
Unit testing and xdebug usage aside, I wish to have a way to throw some browser message is a value is not expected to be present.
Let's say: $className = 45;
If we have:
public function setMainClass($className) {
if (is_string($className)) {
$this->_mainClass = $className;
} else {
echo Yii::trace(CVarDumper::dumpAsString($className),'vardump');
}
}
We will get this output to the browser on development stage.
It's great.
I'm not sure however, if this is a proper way of use Yii::trace of if I'm miss using it.
Please advice.
It is not necessary to echo the call Yii::trace() (it returns void so the echo does nothing). The other recommendation is that you might consider changing category to resemble a path alias as discussed in the documentation. For example-
} else {
Yii::trace(CVarDumper::dumpAsString($className), 'application.models.MyGreatModel');
}
I have a method whose purpose is to retrieve collection items.
A collection can contain a mix of items, let's say: pens, pencils, and papers.
The 1st parameter allows me to tell the method to retrieve only the itemTypes I pass (e.g, just pens and pencils).
The 2nd parameter flags the function to use the collection's default item types, instead.
getCollectionItems($itemTypes,$useCollectionDefaultItemTypes) {
foreach() {
foreach() {
foreach() {
// lots of code...
if($useCollectionDefaultItemTypes) {
// get collection's items using collection->itemTypes
}
else {
// get collection's items using $itemTypes
}
// lots of code...
}
}
}
}
What feels odd is that if I set the $useCollectionDefaultItemTypes to true, there is no need for the function to use the first parameter. I was considering refactoring this method into two such as:
getCollectionItems($itemTypes); // get the items using $itemTypes
getCollectionItems(); // get the items using default settings
The problem is that the methods will have lots of duplicate code except for the if-statement area.
Is there a better way to optimize this?
Pass in $itemTypes as null when you're not using it. Have your if statement check if $itemTypes === null; if it is, use default settings.
If this is php, which I assume it is, you can make your method signature function getCollectionItems($itemTypes = null) and then you can call getCollectionItems() and it will call it as if you had typed getCollectionItems(null).
It's generally a bad idea to write methods that use flags like that. I've seen that written in several places (here at #16, Uncle Bob here and elsewhere). It makes the method hard to understand, read, and refactor.
An alternative design would be to use closures. Your code could look something like this:
$specificWayOfProcessing = function($a) {
//do something with each $a
};
getCollectionItems($processer) {
foreach() {
foreach() {
foreach() {
// lots of code...
$processor(...)
// lots of code...
}
}
}
}
getCollectionItems($specificWayOfProcessing);
This design is better because
It's more flexible. What happens when you need to decide between three different things?
You can now test the code inside the loop much easier
It is now easier to read, because the last line tells you that you are "getting collection items using a specific way of processing" - it reads like an English sentence.
Yes, there is a better way of doing this -- though this question is not an optimization question, but a style question. (Duplicated code has little effect on performance!)
The simplest way to implement this along the lines of your original idea is to make the no-argument form of getCollectionItems() define the default arguments, and then call the version of it that requires an argument:
getCollectionItems($itemTypes) {
foreach() {
foreach() {
foreach() {
// lots of code...
// get collection's items using $itemTypes
}
// lots of code...
}
}
}
getCollectionItems() {
getCollectionItems(collection->itemTypes)
}
Depending on what language you are using, you may even be able to collapse these into a single function definition with a default argument:
getCollectionItems($itemTypes = collection->itemTypes) {
foreach() {
foreach() {
foreach() {
// lots of code...
// get collection's items using $itemTypes
}
// lots of code...
}
}
}
That has the advantage of clearly expressing your original idea, which is that you use $itemTypes if provided, and collection->itemTypes if not.
(This does, of course, assume that you're talking about a single "collection", rather than having one of those foreach iterations be iterating over collections. If you are, the idea to use a null value is a good one.)