How to increment local variable inside Razor Syntax, MVC3 - asp.net-mvc

I am working on MVC3, i have a situation where i want to do something like this:
<Div>
#Code
Dim i = 1
End Code
....
some where, i want to increment i's value, expect 'i' value should be incremented by 1 for subsequent use.
#i = #i + 1
..
</div>
but razor is throwing wrong syntax error message. Could someone help me how to do this properly in side razor code.
Thank you,
Rey.

I don't know VB, but in C# u can use
#{i = i + 1;}
or
#{ i++; /* or i += 1; */ }
UPDATE:
I think in VB must be:
#Code
i = i + 1
End Code
test it!
UPDATE:
I create a MVC3 app with VB and test this code:
#Code
ViewData("Title") = "Index"
Dim i = 0
End Code
<h1>#i</h1>
<h2>Index</h2>
#Code
i = i + 1
End Code
<h1>#i</h1>
It works! post your markup, if u can.

Related

How to refactor repeating code in Pandoc lua filter

I am in the process of writing a Lua filter for Pandoc specifically targeting Pandoc generated LaTeX documents.
Here is a small code snippet from my filter:
LATEX_CODES_FOR_TAGS = {
Span = {
sans = '\\sffamily ',
serif = '\\rmfamily ',
},
Div = {
-- Font Styles
sans = '\\begin{sffamily}',
serif = '\\begin{rmfamily}',
}
}
function Span(span)
for tag, code_for_class in pairs(LATEX_CODES_FOR_TAGS) do
for class, code in pairs(code_for_class) do
if tag == "Span" then
if span.classes:includes(class) then
table.insert(span.content, 1, pandoc.RawInline('latex', code))
end
end
end
end
return span
end
function Div(div)
for tag, code_for_class in pairs(LATEX_CODES_FOR_TAGS) do
for class, code in pairs(code_for_class) do
if tag == "Div" then
if div.classes:includes(class) then
local code_end = code:gsub('begin', 'end')
table.insert(div.content, 1, pandoc.RawBlock('latex', code))
table.insert(div.content, pandoc.RawBlock('latex', code_end))
end
end
end
end
return div
end
As you can see the code for the Div() and Span() functions are almost exactly the same, and therefore want to refactor the code.
I have hitherto been unable to come up with a solution that makes the filter without the repeating code.
I am still very new to Lua and wrapping my head around the concepts.
Thanks in advance.
Define the body of the loop as a separate function:
local function create_handler(tag_str, handler_func)
return
function (obj)
for tag, code_for_class in pairs(LATEX_CODES_FOR_TAGS) do
for class, code in pairs(code_for_class) do
if tag == tag_str then
if obj.classes:includes(class) then
handler_func(obj, code)
end
end
end
end
return obj
end
end
Span = create_handler("Span",
function(span, code)
table.insert(span.content, 1, pandoc.RawInline('latex', code))
end)
Div = create_handler("Div",
function(div, code)
local code_end = code:gsub('begin', 'end')
table.insert(div.content, 1, pandoc.RawBlock('latex', code))
table.insert(div.content, pandoc.RawBlock('latex', code_end))
end)

Loop for editing shopify collections

I'm pretty new to Ruby scripting and have made a script to loop through product collections in Shopify to change the 'product match condition' on all collections but I can't seem to get it to work at all unless I specify the collection ID.
I keep getting an error saying "NoMethodError: undefined method `disjunctive=' for #" and I can't figure out why. The script I've written is below:
Failed collection loop:
count = ShopifyAPI::SmartCollection.count
page = 1
while count > 0
collectionsEdits = ShopifyAPI::SmartCollection.find(:all,:params => {:page=> page})
collectionsEdits.disjunctive = 'true'
collectionsEdits.save
count = count - 50
page = page + 1
end
Specific collection ID:
collectionUpdate = ShopifyAPI::SmartCollection.find(437592964)
collectionUpdate.disjunctive = 'true'
collectionUpdate.save
Any help would be greatly appreciated!
API Documentation: https://help.shopify.com/api/reference/smartcollection
if Shopify::SmartCollection is just an ActiveRecord model so I'd recommend you to write that like this:
ShopifyAPI::SmartCollection.where(page: page).update_all(disjunctive: 'true')
P.S. in where clause you could provide any hash with your conditions
In your case error occurs because find(:all) return a collection but not a single instance that's why you cannot execute disjunctive = 'true'
So if you still want to write that your way do it like this:
count = ShopifyAPI::SmartCollection.count
page = 1
while count > 0
collectionsEdits = ShopifyAPI::SmartCollection.find(:all,:params => {:page=> page})
collectionEdits.each do |collectionEdit|
collectionEdit.disjunctive = 'true'
collectionEdit.save
end
count = count - 50
page = page + 1
end

How can I clean up data from a Model, and include HTML as well?

In my Rails app, I've got a model called Video, and each video has a rating:integer. The ratings are from 1 to 5. However, when I display them to the user, I want to make them look nice. So for now, using text, I replace a rating of 3 with ***.., for instance.
In my video.rb I've got:
def graphical_rating
stars = ''
no_stars = ''
rating.times { stars += '*' }
(5 - rating).times { no_stars += '.' }
return stars + no_stars
end
Now, I want to add some HTML to it, so that it outputs <span class="stars">***</span><span class="no-stars">..</span> but it outputs as text rather than HTML.
What's the best way of handling this so that it's still as easy as it was before (I could just use video.graphical_rating) while being able to also include HTML?
This method actually belongs in your VideoHelper file because it only only applies to your views, not the logic of the model.
This should work:
def graphical_rating(rating)
stars = ''
no_stars = ''
rating.times { stars += '*' }
(5 - rating).times { no_stars += '.' }
return raw("<span class='stars'>#{stars}</span><span class='no-stars'>#{no_stars}</span>")
end
Then, in the view you can call it with:
<%= graphical_rating(#video.rating) %>

Simple string concatenation in rails in view page

I am new to Rails. Can someone please explain to me the concept of string concatenation using variables in view page and the controller?
For example :
In controller Code :
def show
#firstname = 'Test'
#lastname = 'User'
end
In view page :
Full Name : <%= "#{#firstname} #{lastname}" %>
For further details Click Here
Scenarios:- If you want to keep two variables on View page and add concatenation for those then use of space is necessary.
View page:
<%
var string_1 = "With"
var string_2 = "Rails"
var addition_1 = string_1 + string_2;
var addition_2 = string_1 + " " + string_2
%>
<h1> First Addition -> #{addition_1} </h1>
<h1> Second Addition -> #{addition_2} </h1>
Output :
First Addition -> WithRails
Second Addition -> With Rails
in view
<%
var1 = "ruby"
var2 = "on"
var3 = var1 + var2
%>
Finally
<% f_var = "Ruby #{var3}"%>
but this type of code is not recommended in view as it does not look good. You should use helper method for this type of requirement

Importing and using groovy code in GSP

I am trying to use a groovy function inside a GSP. Please help as I am about to tare my hair out here.
At the top of my GSP i have <%# page import = company.ConstantsFile %>
Inside my GSP I have
<p>
I have been in the heating and cooling business for <%(ConstantsFile.daysBetween())%>
</p>
and my ConstantsFile.groovy
package company
import static java.util.Calendar.*
class ConstantsFile {
def daysBetween() {
def startDate = Calendar.instance
def m = [:]
m[YEAR] = 2004
m[MONTH] = "JUNE"
m[DATE] = 26
startDate.set(m)
def today = Calendar.instance
render today - startDate
}
}
I have also tried changing renter to puts, system.out, etc but that isn't my main problem.
Error 500: Internal Server Error
URI
/company/
Class
java.lang.NullPointerException
Message
Cannot invoke method daysBetween() on null object
So I try
<p>
I have been in the heating and cooling business for <%(new ConstantsFile.daysBetween())%>
</p>
but then i get
Class: org.codehaus.groovy.control.MultipleCompilationErrorsException
unable to resolve class ConstantsFile.daysBetween # line 37, column 1. (new ConstantsFile.daysBetween()) ^ 1 error
Please someone help me or point me to a website that shows what to do.. I have tried googling and everything talks about a g:select or some other kind of tag... I just want to output the result of the function like I used to in the JSPs.
First, your GSP's import should be:
<%# page import="company.ConstantsFile" %>
Second, your daysBetween should be static (it makes more sense) and you don't render from anything but a controller:
class ConstantsFile {
static daysBetween() {
def startDate = Calendar.instance
def m = [:]
m[YEAR] = 2004
m[MONTH] = "JUNE"
m[DATE] = 26
startDate.set(m)
def today = Calendar.instance
return today - startDate
}
}
Third, access it in the following way:
<p>I have been in the heating and cooling business for ${ConstantsFile.daysBetween}</p>
And lastly, you should use a taglib for this. I'm editing my post now to add an example
class MyTagLib {
static namespace = "my"
def daysBetween = { attr ->
out << ConstantsFile.daysBetween()
}
}
Then use in your GSP
<p>I have been in the heating and cooling business for <my:daysBetween /></p>

Resources