0

We can use <text> tag in Razor to print the text context in some Iteration or want to display some dynamic text.

Confusion

We can do the above without using the tag also.

Question What is the basic benefit to use <text> that is not common while comparing with other techniques to display text?

Please let me know in case there is any problem in understanding the question.

leppie
  • 115,091
  • 17
  • 196
  • 297

2 Answers2

1

The main benefit is to make your code clean, declarative and self-explanatory. Also <text> tag can eliminate some inconveniences in HTML/C# context switching (without it you will be forced to use @: to indicate context switching).

So, <text> is more like a syntax sugar not some required thing.

Consider the following code snippet. It is invalid and you will end up with exception if try to run it.

@foreach (var item in Enumerable.Range(0, 10))
{
    user
}

We have two options to deal with this issue. They are both valid but first seems to be more consistent and declarative.

@foreach (var item in Enumerable.Range(0, 10))
{
    <text>user</text>
}

@foreach (var item in Enumerable.Range(0, 10))
{
    @:user
}
  • **Also tag can eliminate some inconveniences in HTML/C# context switching (without it you will be forced to use @: to indicate context switching)** Please explain this. –  Jul 27 '13 at 11:58
  • I've edited my post to show you the example of what I have meant. – Oleksandr Kobylianskyi Jul 27 '13 at 12:49
  • @foreach (var item in Enumerable.Range(0, 10)) { user } - here, if you don't put the tag, compiler will assume the whole block is C#, try to find a variable user which does not exist, throws error. The tag is to say clearly that 'user' is just a text which we want in our html. – Arghya C Jul 27 '13 at 13:00
  • @ArghyaC : This coould be done in this way? `

    user

    ` inside the loop?
    –  Jul 27 '13 at 15:35
  • @AlexK : This could be done in this way? `

    user

    ` inside the loop?
    –  Jul 27 '13 at 15:37
  • @SOUsers: Yes. It could be done this way too. – Oleksandr Kobylianskyi Jul 27 '13 at 15:39
  • So, what is the big deal in using ? What is important usage ? –  Jul 27 '13 at 15:45
  • 1
    Yes, it can be done with

    , but that will put each text in a paragraph (with extra spacing and all) - which you might not want. See, the point is, if you already have some html tag, compiler will understand that as html and treat properly. But, in cases like the example by @AlexK where you don't want extra html elements, just want to put some plain text - there you use to distinguish it from C# codes.

    – Arghya C Jul 27 '13 at 15:49
  • @ArghyaC : Thanks and now I have understood. Can you tell the meaning of this line in first paragraph ? **without it you will be forced to use @: to indicate context switching** –  Jul 27 '13 at 16:00
  • That means, you can do the same thing with @: instead of tag. They do the same thing, make C# compiler differentiate html from code block. looks more html-ish & is more handy in cases where you have multi-line text. Refer this http://weblogs.asp.net/scottgu/archive/2010/12/15/asp-net-mvc-3-razor-s-and-lt-text-gt-syntax.aspx – Arghya C Jul 27 '13 at 16:08
  • @ArghyaC Can you tell one unique thing about `` –  Jul 27 '13 at 16:16
  • @ArghyaC : Can you tell what context switching means in this answer ? –  Jul 27 '13 at 16:17
  • Since this comments thread is pieces of related info, I'd rather post it as an answer. – Arghya C Jul 27 '13 at 16:21
  • @ArghyaC : Sure. i am waiting for that. Thanks a lot. –  Jul 27 '13 at 16:27
1

@foreach (var item in Enumerable.Range(0, 10)) { user } - here, if you don't put the tag, compiler will assume the whole block is C#, try to find a variable user which does not exist, throws error. The tag is to say clearly that 'user' is just a text which we want in our html.

It can be done with <p>, <span> or any other existing html tag, but that will put each text in a paragraph (with extra spacing and all) or other markups - which you might not want. See, the point is, if you already have some html tag, compiler will understand that as html and treat properly. But, in cases like the example by @AlexK where you don't want extra html elements, just want to put some plain text - there you use to distinguish it from C# codes.

The first line in the other answer, you can do the same thing with @: instead of tag. They do the same thing, make C# compiler differentiate html from code block. looks more html-ish & is more handy in cases where you have multi-line text. Refer ScottGu's blog

Context switching here means going back and forth between C# code block and html markup while generating a razor view page. The code we write for asp.net mvc views are combination of C# and html codes e.g.

1.  <ul>
2.      @foreach (var p in persons)
3.      {
4.          <li>
5.              @p.Name
6.              <a href="some_url/@p.Id">details</a>
7.          </li>
8.      }
9.  </ul>

where line 1, 4, 7, 9 are html, 2, 3, 5, 8 are C# and line 6 is both. So while compiling, the compiler has to clearly understand what is code and what is markup! So, based on some symbols (@ for code, tags for html etc) compiler goes back and forth from C3 to html and create a final html.

The unique thing about the <text> tag is, it is not a standard html element, but can be used in razor view in the same way. It just encloses a block of plain text that will be rendered in generated html without any html markup.

Arghya C
  • 9,805
  • 2
  • 47
  • 66
  • First of all, +1 for the help. –  Jul 27 '13 at 16:31
  • Can you explain more in details about this line. **Context switching here means going back and forth between C# code block and html markup while generating a razor view page.** . I just need a theoretical knowledge about this sentence. Thanks. –  Jul 27 '13 at 16:37
  • Edited the answer to add some more explanation. – Arghya C Jul 27 '13 at 16:52
  • Compiler create a final html ? –  Jul 27 '13 at 17:04
  • Yes. At run time, actual html is rendered depending on data. – Arghya C Jul 27 '13 at 17:15
  • check here http://stackoverflow.com/questions/17406582/context-switching-in-isreusable-property can you elaborate this ? –  Jul 29 '13 at 06:12
  • sorry wrong link last time –  Jul 29 '13 at 06:12